x


Cycle and Instantiate from an Array of GameObject

Hi there, I'm trying to create a function that allows (with the mouse click) to instantiate a gameObject from an array... whilst destroying the previous, in other words cycling through an array of gameObjects.

var childObject : GameObject[];
var changeObj : int = 1;

function onScrollChangeObject(){
    var newObj = Instantiate(childObject[changeObj],transform.position,transform.rotation);
    //I want the new object to be the child of this object (with the script)
    newObj.transform.parent = transform;
}   

function Update () {
    if (Input.GetButtonDown("Fire1")){
            //Is it possible to use a 'for' loop to do this cycle
        if(changeObj<2){
            changeObj++;
        } else {
            changeObj = 0;
        }
        onScrollChangeObject();
}

This instantiates from an array, but I can't work out how to Destroy the previous

more ▼

asked Apr 27 '11 at 11:51 AM

Caiuse gravatar image

Caiuse
787 83 104 122

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

1 answer: sort voted first

Instantiate returns a reference to the instantiated object. As long as you have only one object alive at each given time you can save that reference in order to destroy it, and do something like this:

var childObject : GameObject[];
var changeObj : int = 1;
var currObjAlive: GameObject;

function onScrollChangeObject()
{
    // If there is already an object alive- destroy it
    if (currObjAlive)
        Destroy(currObjAlive);

    currObjAlive = Instantiate(childObject[changeObj],transform.position,transform.rotation);
    //I want the new object to be the child of this object (with the script)
    currObjAlive.transform.parent = transform;
}   

function Update () 
{
    if (Input.GetButtonDown("Fire1"))
    {
            //Is it possible to use a 'for' loop to do this cycle
        if(changeObj<2){
            changeObj++;
        } else {
            changeObj = 0;
        }
        onScrollChangeObject();
}

BTW - a nifty trick to use in situation where you have an array and you want to iterate over it using the index and loop is:

if (Input.GetButtonDown("Fire1"))
{
    changeObj = (changeObj + 1) % 2;
    onScrollChangeObject();
}
more ▼

answered Apr 27 '11 at 11:59 AM

Cyb3rManiak gravatar image

Cyb3rManiak
1.6k 1 4 17

That little trick is something i've been needing for ages! and that solution worked perfectly. Thankyou!

Apr 27 '11 at 12:18 PM Caiuse

Small side note: instead of going crazy saving the instantiated object reference just do oldObject = newObject; newObject = Instantiate(stuffz). Now you can destroy the old object or do.. what ever. ;)

Apr 27 '11 at 12:34 PM Joshua
(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:

x1683
x95
x37

asked: Apr 27 '11 at 11:51 AM

Seen: 1338 times

Last Updated: Apr 27 '11 at 11:51 AM