Help! Accessing variables on the fly...

Basically I’m trying to get reference to a variable within the same script, but I’m not sure if I can go about it this way:

I have 3 GameObjects: startPos1, startPos2, startPos3.

I just want to pass in a number (1,2,or3) to create a variable the refernces the appropriate gameObject var.

In flash it would be something like:

setVar(1);

function setVar(n:Number)
{
var activeObj = this[“startPos” +n];
}

I’m not sure how to go about doing this in javascript though…

Easiest and fastest is to not try constructing names like that, but use an array instead.

var startPos : GameObject[];

Then you refer to the desired one like “startPos[0]”, “startPos[1]”, etc. Although given the name of the variable, it’s likely it would be a little more convenient to define it as Transform rather than GameObject.

–Eric