x


How to instantiate a game object few times and then destroy the clones one by one?

I'm trying to be able to instantiate a game object from the script attached to a different object, so i could later destroy them one by one. How do i keep track of number of objects and how do i access a single object to be destroyed? Do i need to make my object a prefab? Do i need to use an array for this (i need a max of 6 objects)?

I think i can't use a script in the object to clone, because the cloning would be made from gui menu, so every clone of my object would also create a cloned menu on top of the previous one. This is just too complicated for me.

more ▼

asked May 16 '12 at 08:41 PM

gratz gravatar image

gratz
0 2 3 4

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

This got me the idea how to proceed. Thank you very much.

more ▼

answered May 17 '12 at 05:58 PM

gratz gravatar image

gratz
0 2 3 4

(comments are locked)
10|3000 characters needed characters left

You can make a list of GameObject and when they're instantiated (Instantiate a prefab) you add them to the list. To destroy the objects you can use a loop inside a coroutine, like:

IEnumerator destroyMyObjects(System.Collections.Genenic.List<GameObject> whichObjects, float delayBetweenDestructions) {

for(int i = 0; i < whichObjects.Count; i++) {

Destroy(whichObjects[i]);

yield return new WaitForSeconds(delayBetweenDestructions);

}

}

Make sure to call the method with StartCoroutine just one time.

You can make the list of Game Objects a static list so you can add in to this list from any other script.

more ▼

answered May 16 '12 at 08:55 PM

GutoThomas gravatar image

GutoThomas
593 32 46 47

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1672
x764

asked: May 16 '12 at 08:41 PM

Seen: 300 times

Last Updated: May 17 '12 at 05:58 PM