- Home /
FindGameObjectsWithTag not finding a tagged object
Can't we FindGameObjectsWithTag within indestructible objects (made so through DontDestroyOnLoad)?
If that's not the issue, what could be turning the method unable to find a tagged object?
I have a tagged object perpetuated through many scenes which was working fine within each scene before, with the same FindGameObjectsWithTag... But since I've moved it into an indestructible object so it's replicated in every scene (and thus removed each instance from each scene), it stopped working. I'm a little bit at loss here, so any help is welcomed! Specially if it's answering the two questions above! :)
Sorry if this sounds obvious but try pausing the game (in editor) and check if your object exists in the scene with the tag you´re looking for...
@cj Pausing is too obvious, that's why it can be missed sometimes, specially by me who almost never uses it... But yeah, I've checked if the object exists. If this question stays open long enough I at least have a good reason to believe it's just bad code. - And by the way, I just checked myself the answer to the first question would be "sure can, it works fine and of course that has no relation". As expected. :)
Answer by Aleron · Dec 22, 2011 at 05:34 PM
Without seeing your code, at any point are you setting the object inactive? FindGameObjectsWithTag only finds objects that are active in the scene, so if you are disabling them by setting them inactive, that would explain why the method no longer finds it.
As far as I'm aware, the only find method that will return inactive objects is Resources.FindObjectsOfTypeAll, which probably isn't ideal. Your best bet if the object is being set inactive is to maintain a reference to it somewhere in another script that is always active, then perform your operations on that.
Haha, there you go. As obvious as @cj expected, just not exactly what he touhght! Thanks!!
You're welcome. I banged my head against that particular wall a few too many times before I learned that the GameObject.Find* methods only return active objects.
Yeah, but I'm way too stupid. I actually already knew about this issue and I even had already referenced it. :P
Answer by OZAV · Feb 02, 2018 at 03:31 PM
Now from Unity 2017 v3.0 here, it does not find even active objects by the tag, for an instance "Player" tag can not be registered in the tag editor either, or found ingame anymore, and in 2017 v2 - it was all working ok ... This is why i stopped paying to unity by my studio - every odd version like V1, V3 etc of 2017 is LOADED with bugs ... It's not worth paying for.
Answer by That1phillps · Jan 03, 2020 at 06:42 AM
If Object A is labeled with tag "A" and is the child of object "B" that is labeled with the tag "B", you will not find Object A by tag. At least that has been my experience.
you solved my problem but i dont know why it is ?
Answer by Mahamed9628 · Dec 22, 2011 at 10:10 PM
Do you instantiate your object from Resources folder (or from assets)? Make sure that object's tag is setted in that folder or add script that would set tag at the start of new scene (in awake function).
Huh?! This has nothing to do with Resource, folders or instantiate. At least as far as I know. I never even knew we can set tags to folders, but I don't see how! Sorry to say this seems like an unfunded answer to me. Still, thanks for trying to help.
You don't know that you can set object's tag in assets folder? For example: we have dropship that is disposed as prefab in resource folder. It has tag "ship". And when we're instantiating this dropship, it's already tagged. It's what I meant.