I wanted to be able to call a method in the head script from the apple script in my snake game remake in Unity, but when I use [SerializeField] Head Head, it doesn't work and brings up an error saying that there isn't that GameObject even thought the name of the other GameObject is Head and it works when I call a different GameObject, but it doesn't work with the Head GameObject. I tried using variable type GameObject, but Head.Eat(); was still giving the error that they can't find the method even though I had the method public void Eat(){} with no errors in that script or that GameObject.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Apple : MonoBehaviour { [SerializeField] Text scoreText; [SerializeField] Player player; private int Score = 0; private void OnTriggerEnter2D(Collider2D collision) { Destroy(gameObject); Score++; player.Grow(); scoreText.text = Score.ToString(); } }
The error I got is that even though there is a Player GameObject with a script named player with a grow function in it, for some reason, Unity gives me the error AssetsScriptsApple.cs(9,30): error CS0246: The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?) Even though the GameObject Player is right there with no changes in capitalization or anything else, and Unity still gives the error and can't recognize it. I tried to use [SerializeField] GameObject Player instead, but after using that, when I use Player.Grow, I still get an error which says that there is no such function. Edit: I tried using [System.Serializable], but it still gives the same error.
question from:https://stackoverflow.com/questions/66052679/unity-serializefield-head-head-doesnt-work-in-the-script