GameObject.Find returning null

Hey! I’m fairly new to Unity & C#, but I have completed my first project ( a small crappy space-invader esque game).

In making my second game, I’ve been using the code from my old game, as a reference a lot, which has been working fine. until, I tried to use “GameObject.Find” for some reason, even though I copied my code completely from my old script, it won’t work…

Could anyone point me in the direction, of why Unity would think I haven’t set “gManager” to a value?

public class bulletMove : MonoBehaviour {

    private float speed;
    private Vector3 movement;
    public GameObject gManager;
    gameManager gameManager;

    void Awake()
    {
        Destroy(gameObject, 6f);
    }

	void Start () {
        speed = 2;
        movement = new Vector3(1f, 0f, 0f);

       GameObject gManager = GameObject.Find("gManager");
       gameManager = gManager.GetComponent<gameManager>();
	}
	

	void Update () {

        gameObject.transform.Translate((movement * speed) * Time.deltaTime);
	}

    void DestroySelf()
    {
        Destroy(gameObject);
    }

     void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            Debug.Log("So");
            Destroy(gameObject);
            if (gManager != null)
            {
                gManager.SendMessage("TakeHealth");
            }
        }
    } 
}

For a note, there is definitely an empty object in my hierarchy called gManager.

Thanks in advance!

  • Make sure that “gManager” is active in hierarchy. “Find()” method does not find inactive objects.

  • Are you sure that “gManager” is null? Because maybe gManager is not null and this line is causing error:

    gameManager = gManager.GetComponent();

Maybe “gameManager” component is not attached with “gManager” game object.