I use the GameObject tag_object=FindGameObjectsWithTag(“tag”); to determine if an object exists. If no such object exists, tag_object.Length should be 0. But sometimes tag_object.Length returns 1 although no such object exists. I can’t even find it in the Hierarchy window.
Is this a bug of unity? I am using version 5.0.1f1.
Two years later and this problem still exists, and it’s not a little problem! I’m using Unity 2017.1.0f3
Run a game and destroy all found gameobjects via script. Then turn off or remove your script. After that, you can normally launch a game, and these objects won’t be found. I don’t know for how long it works.
var objectsWithTag = GameObject.FindGameObjectsWithTag(Tag);
for (int i = objectsWithTag.Length - 1; i > -1; i--)
I have a scene with Object Pooling, that instantiates previous set amount of gameobjects with a given tag, but also finds gameobjects with a given tag that are already on a scene and put them to the pool. So the game designer can put for example a few knights on a scene, and they will be in the pool after scene starts. Of course first I’m looking for all gameobjects that are on a scene, and then I instantiate the rest (if an amount of them is still lower than I wanted).
to find all objects with given tag on a scene, and then I get only the root gameobjects (in case that childrean has the same tag, and they are also found), because this is all I need.
Normally everything works great, there are no problems. But sometimes, one type/prefab of gameobjects goes crazy. The method FindGameObjectsWithTag finds gameobjects with given tag that does not exists on a scene (in a hierarchy) and they never exists! It’s even more weird. At first, these gameobjects has normall name like hips/legL/legR/etc. but also a little different like KnightRagdoll(Clone)AnimatorPreview(…). I don’t even have an object like that in my project (I mean with this “AnimatorPreview” part).
Then I did change the name of this found gameobjects in code after restarting a game, like this:
foreach (var obj in objectsWithTag)
obj.name = “WhoAmI”;
When I try to find this objects in a scene hierarchy, I see nothing.
Then I removed the code that changes the names of these gameobjects, and restearted a game again. And I found in my logs, that these objects are still named “WhoAmI”! But… How?! I do not change names to “WhoAmI” anymore, but these objects have this name!
I do not havy ANY line with Destroy() in my code, and only ONE line with Instantiate, so it’s impossible that I’ve missed something
I’ve spent all night on this and it’s really late now, I hope that my post is understandable
It probably isn’t a bug in Unity. A lot of projects use that function and they wouldn’t work as expected if some times it returns objects that don’t exist.
If you can’t see the object maybe your code deleted it after getting the reference, or maybe it’s a child of another object.
Try setting a break when the tag_object array is not empty:
if (tag_object.Length > 0)
This will pause the game at that exact moment and you can check the hierarchy.
I’m also getting this strange issue…in code this exists but exists nowhere in hierarchy…i am able to debug and it belongs to a DIFFERENT SCENE that any that are live and ACTIVE…and the scene doesnt exist in the hierarchy.
If you look at the gameObject.scene for the objects that DONT match the current scene the path is “” and GUID is all zeros! Unity is holding something in memory that has long since been deleted.