- Home /
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!
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"
@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
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.
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().
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.
Ah, rookie mistake. Apparently there was another object in my scene with that tag. Thanks!
Your answer
Follow this Question
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