Yangrui
Yangrui

Reputation: 1247

How to destroy an instantiated(cloned) object in unity3d?

I am working with Unity3d to create an iPhone game. Because iPhone is highly limited in performance than PC, I want to keep things economic. In my game I have a very long stairway and there is a character walking on it. If the character is above one step some height, the step will be destroyed. But i could not get a reference to a single gameObject. How could I achieve this? Thank you very much!

function buildFirstStair () {
    for (var y = 0; y < 80; y++) {
        for (var x = 0; x < 80; x++) {
            if (x == y) {
                var step = Instantiate(cube, Vector3(0, x*0.25, y*0.25), Quaternion.identity);
            }
        }
    }
}

Upvotes: 1

Views: 9709

Answers (1)

Pierre-Emmanuel
Pierre-Emmanuel

Reputation: 343

You can put all instance in a List, and delete them after. Something like:

function Create()
{
    List<GameObject> mylist;

    for (float y = 0; y < 80; y++)   
        for (float x = 0; x < 80; x++) {
            if (x == y) {
                mylist.push((GameObject) Instantiate(...), Quaternion.identity));
            }
        }
}

And to delete you do something like

foreach object in mylist
{
  Destroy(object);
}

Sorry for c# code but I'm a c# user.

Upvotes: 5

Related Questions