- Home /
Problem is not reproducible or outdated
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?
Bear in $$anonymous$$d that GetComponent returns null if no such component is attached at the time of the call. The fact that lastPlayerSighting
is null would seem to indicate that GetComponent didn't find a component attached to the GameObject you're asking about. $$anonymous$$aybe it's attached later, or attached to a child object?
Answer by smoggach · Sep 04, 2014 at 07:56 AM
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.
Thanks for your answer Smoggach, unfortunately though, this makes no difference. If it was an issue of the gameController object not having been created yet (all objects are created before Awake() functions are called anyway) then GameObject.Find("gameController")
would run into the same issue (although I tested it anyway, and putting it into the Start() function didn't solve the issue). It seems that there's something wrong with the actual GameController default tag, as re-tagging it as something else (anything else) solved the issue. That still leaves the question: What's wrong with the GameController tag?
Follow this Question
Related Questions
GameObject.FindGameObjectWithTag not finding the tagged objects. 1 Answer
Waypoint Arrow not pointing at tagged object ? 1 Answer
Looking for gameObjects in the update function 2 Answers
Terrain Tag OnControllerColliderHit not showing 0 Answers
Swapping a tag for string not working, replacing a tag with a public string, help. 1 Answer