FindGameObjectWithTag failing

Hi all, sorry for what I know must be a repeat question, but damned if I can find the answer.

Following along the Stealth tutorial from the Unity website. I’ve already circumvented a few errors which I’ve finally realised was actually because FindGameObjectWithTag wasn’t returning my gameController object.

My scene contains an empty object called “gameController” tagged with the default “GameController” tag, and a player tagged with the default “Player” tag.

void Awake()
{
	player = GameObject.FindGameObjectWithTag("Player");
	lastPlayerSighting = GameObject.FindGameObjectWithTag("GameController").GetComponent<LastPlayerSighting>();
}

void OnTriggerStay(Collider other)
{
	if(renderer.enabled)
		if(other.gameObject == player)
			lastPlayerSighting.position = other.transform.position;
}

When I play my game and run my character into the collision box, it stops my game and throws a NullReferenceException at that final line of code. Changing the lastPlayerSighting line to ...GameObject.Find("gameController")... (and keeping the player line exactly as is) fixes all my issues and the game runs perfectly, but WHY!?

I’ve checked my spelling, capitalisation and tags about 20 times and I’m eleventytwelve percent sure it has the right tag, why is FindGameObjectWithTag("GameController") not working when it’s working for player?

Try using GameObject.FindGameObjectWithTag from your Start() function instead of Awake().
In Awake() there is no way to know which objects have been created yet. In Start() everything’s already created. You may want to look into singletons as well.