• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 02, 2017 at 05:38 AM by James-Blaskett for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by James-Blaskett · Sep 03, 2014 at 09:50 AM · taggamecontrollerfindgameobjectwithtag

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · Sep 05, 2014 at 04:08 AM 0
Share

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?

1 Reply

  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image James-Blaskett · Sep 05, 2014 at 02:50 AM 0
Share

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?

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObject.findGameObjectsWithTag returning empty? 1 Answer

FindGameObjectsWithTag works only to first tagged 1 Answer

Get a total count of gameobjects with the same tag and change level if score reaches max score! 0 Answers

difference between GameObject.FindGameObjectWithTag(Tag tag) and GameObject.FindWithTag(Tag tag) 3 Answers

What is GameController tag used for? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges