Hi Unity Community.
I would like to make a list of objects in a scene. Lets say I design 3 space levels and I have a perfab called "commet". In level 1, I add 10 commets, in lvl two, 3 commets. And in level three, 12 comments.
Thank you for your time.
asked Sep 06 '10 at 02:09 PM
You could just have a static variable that increments/decrements every time you destroy a comet. It seems as though you know how many comets there are in total at any given time. When you spawn more, you'd just add that same number to the current total.
When you spawn comets:
When you destroy a comet:
Your GUI script could then just display cometsDestroyed/cometsTotal.
However, since you asked for a function that creates an array and counts them, this function will count how many GameObjects with the 'Comet' tag are still existing. The GameObject.Find searches aren't cheap, so you probably wouldn't want to do this every frame.
Honestly, I would suggest the first approach, unless there's a reason you need the function (that you didn't mention in your question).
Hope this helps.
answered Sep 06 '10 at 02:24 PM