call variable from string?

Hi everyone, i’m new to unity and javascript. since now, sorry for my bad english :wink: .

i’ve the following script (called inventory) that’s just a reference for the objects

 static var object1 = 0;
 static var object2 = 0;
 static var object3 = 0; 

and another script where i’ve an integer input (1,2,3).
i need to know how to modify inventory.variables from this script
something like

inventory.(object+input) = 1;

there’s a way to call a variable from a string + an integer value? or just a string?

thanks everyone :slight_smile:

In short you cannot do this in this fashion. What you are probably needing is either an array or hashtable.

This is an example of using an array:

var myobject = [10,11,12];

myobject[2]=5; // NOTE that the index of 2 is the the THIRD value

Debug.Log(myobject[0]);
Debug.Log(myobject[1]);
Debug.Log(myobject[2]);

This is an example of using a Hashtable:

var myrefs = new Hashtable();

myrefs["object1"]=0;
myrefs["object2"]=0;
myrefs["object3"]=0;

var lookupString: String;
var indexNumber=2;

lookupString="object"+indexNumber.ToString();

myrefs[lookupString]=3;

Debug.Log(myrefs["object1"]);
Debug.Log(myrefs["object2"]);
Debug.Log(myrefs["object3"]);

Read up on Arrays and Hashes on:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

Note that Unity has two types of arrays and its sometimes confusing so be aware