FindWithTag doesn't seem to be working on one of my objects. What am I doing wrong? Code (CSharp): void Start() { controls = GameObject.FindWithTag("Controls"); player = GameObject.FindWithTag("Player"); print(controls); print(player); }
Yes. I created the tag and attached it to the GameObject. It doesn't work for some reason. I can't figure it out.
Well, it should work. Double-check that the objects have the tags applied. EDIT: also make sure that your object is also enabled.
Yeah, but I can replicate your setup here in 10sec and it will work just fine. Either the object is toggled off or something else is wrong with it. But at least your smaller screenshots showed object with tag and the tickbox for enable was also active... hm. Try with other Tag name and see if the result is same? Also it shouldn't matter if you have more than one tagged, it will return one of them.
Verify the Controls GameObject exists in the scene hierarchy and is active at the time you are calling FindWithTag. FindWithTag can't find inactive GameObjects, nor GameObjects which aren't even in the hierarchy. Make sure you're not instantiating this Controls object in some other Start method, where this one just happens to be called before the other one.
Thank you. I had another script that was turning off the object. I changed that and now it is working. Thanks.
I had a similar issue and after some debugging what I found was: I was using using "GameObject.FindWithTag();" twice - once for a health bar, and then for the player. The errors kept showing up mostly for the player not being found, and it was definitely correctly tagged. After awhile I figured out that once "GameObject.FindWithTag()" failed to find the first object with tag (the healthbar), it would then automatically fail to find the next "GameObject.FindWithTag()", even though the 2nd one was definitely tagged correctly. Weird that the errors were saying the 2nd one wasn't found, even though it was the first one that caused the issue