Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by nerdares · Dec 04, 2016 at 09:40 AM · c#gameobjectgetcomponentfindfindgameobjectswithtag

Getcomponent error?

Let's say I have an object A, and an object B.

B contains a script called "Test" right? B has a tag called "Player"

Now in A, on my start method, it does this:

 if (GameObject.FindGameObjectWithTag("Player").GetComponent<Test>() == null)
             print("null");

In this case, A will not print out null because B has that component.

Now let's say I create an object C.

C is the same as b, however its tag will be player2. It still has Test component in it.

object A will now search for a gameobjectwithtag of "player2".

Problem is, null will be printed to the console.

As you can see in this scenario above, it's pretty weird. I'm having this situation right now and I think it might have to do with all the gameobjects scripts being loaded in or something. It's weird because object A can find object B but not object C.

Let me know if I have to clear up on anything, this has been driving me crazy!

Comment
Add comment · Show 4
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 Glurth · Dec 04, 2016 at 09:57 AM 0
Share

object A will now search for a gameobjectwithtag of "player2".

Why do you think it will do that? Your Script A clearly states "Player" in it's code, not "player2"

avatar image nerdares · Dec 04, 2016 at 10:06 AM 0
Share

@Glurth sorry let me be more clear. When I said that object a will search for object c I literately meant as in I went into the code, and changed it to gameobject.findgameobjectwithtag("player2").getcomponet(). Let me also mention that it can find the gameobject but not the component

avatar image Bunny83 nerdares · Dec 04, 2016 at 10:42 AM 0
Share

Are you sure that it finds the correct gameobject? $$anonymous$$aybe there's another gameobject with the tag "player2"?

Try adding a public variable of type GameObject and assign the result of your "FindGameObjectWithTag" to that variable. Now at runtime you can simply left-click once on that variable in the inspector of object A and it will highlight the object it has found.

 public GameObject debugObj;
 
 void Start()
 {
     debugObj = GameObject.FindGameObjectWithTag("Player2");
 }

Once you see which object was "pinged" select it while in run mode to see if that object has a Test component. Because the reason why GetComponent fails could be:

  • FindGameObjectWithTag finds another gameobject that doesn't have the Test script

  • FindGameObjectWithTag finds the correct gameobject but somehow it doesn't have a Test script. Either because you somehow "misplaced" it initially, or because some other script ir the script itself might destroy the script before your object A has a change to access it.

avatar image roman_sedition · Dec 04, 2016 at 01:16 PM 0
Share

Is object C already in the scene? Or is it something you are instantiating into the scene, because object A is only looking for object C in Start().

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AurimasBlazulionis · Dec 04, 2016 at 12:23 PM

Do Debug.Log(GameObject.FindGameObjectWithTag("player2")) and I am pretty sure you will see the wrong game object printed. Check the tags, you might have set the same tag on other object too. Also, try to avoid finding by tag or by name since it is slow and unreliable. The same performance problem happens with GetComponent. These functions are the killers of performance.

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 nerdares · Dec 04, 2016 at 09:43 PM 0
Share

Ah, rookie mistake. Apparently there was another object in my scene with that tag. Thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

254 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating a single-line function for GameObject.Find and GetComponent (for multiple components) 3 Answers

Add and destroy instantiated gameobject in linkedlist C# 1 Answer

error CS0118: `New_Career.Fame' is a `field' but a `type' was expected 1 Answer

Find GameObjects with a true boolean and put them in an array? 1 Answer

Best method to manage multiple tags 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