r/unity • • 14d ago

Coding Help GameObject.FindGameObjectsWithTag gets an array with the same length after one of the objects has been destroyed.

I would have thought that after destroying one of the objects with the relevant tag, the array would become shorter. The array remained the same length after the object was destroyed, so I thought I would paste the same statement at the point in the code after the object has been destroyed. However, it's still the same length, and the entry with that index is now a stale object. Other indexes still refer to the same objects, which I also don't understand. I even tried setting the array size to zero before the GameObject.FindGameObjectsWithTag statement. I want to reset the array to only include objects that still exist. Any help?

3 Upvotes

15 comments sorted by

5

u/Competitive-Elk763 14d ago edited 14d ago

It does only give objects that exist. Your code is incorrect or there are other objects with that tag in the scene.

Note that destroy waits until the end of the frame to trigger, it is actually marking the object for removal in the current frame. So you can't do both on the same frame.

There is DestroyImmediate to immediately destroy but you shouldn't use that.

1

u/MalgorgioArhhnne 14d ago

Why shouldn't I use DestroyImmediate?

1

u/Competitive-Elk763 13d ago

It will break the general chain of events by calling OnDestroy of all of its components before other interactions (e.g., physics, update, etc..) are finished. Easy way to introduce unexpected future problems, so it's considered bad practice.

You also should not really need to remove an object on the same frame.

You can disable something with (gameobject).SetActive(false) and get the same effect. Or, you may want to consider whether or not you really need to use FindWithTag.

1

u/MalgorgioArhhnne 13d ago

I used DestroyImmediate and it worked fine for what I was trying to do. I needed to destroy an object in the array and then reset the array on the same frame.

2

u/TouchSpecialist1739 13d ago

The use of DestroyImmediate to refresh the array of objects returned with FindGameObjectsWithTag is like having a hole in the wall and filling it with Dynamite. You have 2 bugs in your game, you just dont know it yet

1

u/MalgorgioArhhnne 11d ago

Really? Then what are the bugs? If the game is working fine, then there are no bugs.

1

u/TouchSpecialist1739 11d ago

Sure, lets do that..

11

u/vanit 14d ago

Are you using DestroyImmediate? Otherwise references will persist for the rest of that frame. It's probably better that your code just doesn't depend on that functionality.

2

u/HandshakeOfCO 13d ago

This is what’s actually wrong yeah.

OP: don’t get yourself in the business of “playing detective.” Any time you find yourself thinking “I need to FIGURE OUT if this happened….” Send a message instead.

1

u/eminsefa 13d ago

Don’t use it anyway. Use a list (and a helper manager script if needed), add and remove objects to that list when they are enabled-disabled/created-destroyed. Hashlist is better regarding of your need. You never need find methods, don’t use them

Edit: If it’s editor script, it’s fine to use it. Then DestroyImmediate is the answer

1

u/MalgorgioArhhnne 13d ago

I never need Find methods? Half of the code I've written for this project is Find methods.

1

u/eminsefa 13d ago

It’s not a good practice and it’s very bad for performance. It will loop through all the gameobjects in the scene whenever you call it.

To fix this (without any complexity), add single scripts to objects that you need to find and do the subscription I mentioned to a static script’s static lists (to call easier). If there are many tiny objects, create a holder and make it have the script that subscribes those to that static lists. Then you will have way more less objects to search when you try to find one. It will be better more if you use more lists.
To fix this with a decent practice, learn object oriented programming (OOP)

If you are not keen to improve your knowledge or not planning to keep coding and there isn’t performance problem then you may continue using it.

1

u/MalgorgioArhhnne 11d ago

I haven't noticed a performance problem. If I was making a 3D open-world, sure, but I don't think the turn-based RPG I'm making would have this problem. Even for the field-view, where you can walk around and touch enemies to initiate a battle, there would probably be at most 15 enemies in the scene, and not that many other objects. Plus, it's not like I'm using Find methods all the time, anyway.

2

u/johnv_dev 14d ago

destroy doesn't actually remove the object until the end of the frame, so if you call FindGameObjectsWithTag right after it, the dead one is still in there. easiest fix is to call SetActive(false) on it right before Destroy, since FindGameObjectsWithTag skips inactive objects. then the new array will only have the live ones.

1

u/snipercar123 14d ago

It takes one frame to fully destroy a gameObject, could this be something you missed?