- Home /
Checking Instantiate/Destroy has been called | Checking number of scene GameObjects
Hello.
Is there a way to check the number of all game objects in the scene, or to check if Instantiate() or Destroy() has been called, without putting an extra line to every script that does that?
It would help a lot to increase the performance, as I have some scripts that search the scene for specific objects. This way I would not need to check every frame, instead, I'd tell the script to scan again only when needed.
Thanks,
Luka.
Answer by justinl · Oct 15, 2012 at 12:02 AM
You could hold a Singleton GameManager type class. Everytime an object is Instantiated you could add the reference to that GameObject in a global GameObject[] array. Then when that object is destroyed you'd remove that object from the global GameObject[]. This would only require you to write your own Instantiate and Destroy methods which would basically just do that list add/remove and would give you an array with reference to every GameObject currently in your scene.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How to insert an object after Destroy(gameObject) 2 Answers
Having problem with Destroying GameObject 0 Answers
Destroying 2D Array of Instantated Objects 2 Answers
How to make sure only one GameObject is instantiated at a time. 3 Answers