Creating an array of items on startup

Hi, I’m trying to instantiate a set of items and keep a reference to them so that i can move them within the update method.
The problem i’m facing is that only the first item is created.

var baseItem : GameObject;
var array;
for(i=1;i<=10;i++)
{
	 array.Add(i,Instantiate(baseItem,new Vector3(0,0.0375+(0.125-0.0375)*(i-1),0),Quaternion.identity));
}

If i change the loop to

for(i=1;i<=10;i++)
{
var item=Instantiate(baseItem,new Vector3(0,0.0375+(0.125-0.0375)*(i-1),0),Quaternion.identity);
}

i can see al 10 clones being created, but i will lose any reference inside the script…
I think i’m missing something in the value/reference type way in jscript, so if you can give me a clue where the problem is it would be great.

I’m not sure where this code sits in relation to other functions, so I’m not sure if my response makes sense or is helpful. But this is how I’d do the code:

var baseItem : GameObject; 
var array : GameObject[]; //the brackets make this an array

function Start() {
    for(i=1;i<=10;i++) 
    { 
        array.Add(i,Instantiate(baseItem,new Vector3(0,0.0375+(0.125-0.0375)*(i-1),0),Quaternion.identity)); 
    }
}

function Update() {
    //the array should be available here
}

Mmm not working, it says Add is not a member of GameObject.

I should have tested the code first.

var baseItem : GameObject;
var array;

function Start() {
	array = new Array();
    for(i=1;i<=10;i++)
    {
        array.Add(i,Instantiate(baseItem,new Vector3(0,0.0375+(0.125-0.0375)*(i-1),0),Quaternion.identity));
    }
}

function Update() {
    //the array should be available here
}

That oughta do it.

Thanks, it solves it :smiley:

Cool!

Here’s the result ;-D quite good eh?

Dude, that’s fantastic! That’s great! My coworkers liked it (and laughed about it) too. Great idea! And I’m glad I could help!

Is the movement controlled by mouse clicks? Or by arrow/WASD keys?

For now it’s wasd controlled, i’m looking forward to add some kind of 3rd person control. Not soon anyway, it’s friday afterall :smiley: