Can't find the object - NullPointerException

Hello,

I wrote a script to find an object in the world, but something not working right. Here my code:

public class GameManager : MonoBehaviour {
    private GameObject playerObject;
    private Player player;

    void Awake() 
    {
        GameObject playerObject = GameObject.Find("Player");
    }
    void Start() 
    {
        player = playerObject.GetComponent<Player>();
    }
}

But I get a NullPointerException in the player = playerObject.GetComponent<Player>(); line. I can see the player object in the scene view. What’s wrong?

In the Awake method you are assigning the GameObject named “Player” to a local variable playerObject, not the class field with the same name. Try this:

void Awake() 
{
    playerObject = GameObject.Find("Player");
}