Find number of objects in a scene

I am using GameObject.FindGameObjectsWithTag(“ObjectNeeded”).Length to count the number of specific object in a scene. Before this, I use Destroy to remove this specific object. As a result, I find that the number returned is not very accurate.
I guess there are some delay between Destroy function call and actual removal of objects. Is there any better way to do this? Thanks.

1 Answer

1

My advice would be to change the tag of the object you’re destroying, so it won’t be included in the search.

The objects are instantiated in a local function so I do not have reference for them. That's why I use tags to find them and destroy them. Are there still other ways besides defining a list of objects to have reference for them? Thanks.

Cant you use FindGameObjectsOfType to return an array of objects? Won't that work for you.

I guess I misunderstood Alec Slayden's advice. Changing tags before destroying them works for me. Thanks a lot