eval for dynamic objects

I found a previous post wher edynamic names were implemented via Hash Tables. I have the following problem.

I am trying to build a dynamic menu system. My component asks the number of items and the material for each item, then it instaciates an object and assigns the position and material.

Everything went smoooth, I instatitated the items, placed them correctly, etc, but when I wanted to assign a certain material to one of the dynamically created items, I was in trouble!

This is what I did:

var skybox : Material; 
var items:int = 4;
var speed:int = 125;
var itemMaterial1 : Material; 
var itemMaterial2 : Material; 
var itemMaterial3 : Material; 
var itemMaterial4 : Material; 
var itemMaterial5 : Material; 
var itemMaterial6 : Material; 
var itemMaterial7 : Material; 
var itemMaterial8 : Material; 
var itemMaterial9 : Material; 
var itemMaterial10 : Material;

private var angInterval:int = 90;

function Awake () {
	Camera.main.GetComponent(Skybox).material = skybox;
	GameObject.Find("Reflective Water").GetComponent(Skybox).material = skybox;

	angInterval = 360/items;
	var Menu = GameObject.Find("Menu");

	var MenuItem1 = GameObject.Find("MenuItem1");
	GameObject.Find("MenuItem1/Plane").renderer.material = itemMaterial1;


	for (var i=1;i<items;i++) {
		transform.Rotate(0, angInterval, 0);
		var NewItem = Instantiate(MenuItem1, Vector3(0,0.5,3), MenuItem1.transform.rotation);
		NewItem.name = "MenuItem" + (i+1);
		NewItem.transform.parent = transform;
		GameObject.Find("MenuItem" + (i+1) + "/Plane").renderer.material = eval("itemMaterial" + (i+1));
	}

}

Now I need to assign the newly created item a material that to my javascript knowledge can be assigned like eval(“itemMaterial” + (i+1)). Obviously this does not work.

I even tried a Hash but unity cannot asisgn a material object to a string, or something…

Please help.

You might want to just use a builtin arrays instead:

var itemMaterial1 : Material[];

This gives you an array in the inspector which you can dynamically resize in the inspector.

You can access an element like this:

mesh.renderer.material = itemMaterial1[2];

Thanks. Seems that you guys got most of the eval situaltions covered for now.

One question. I read that arrays in unity are based upon .net arrays. I also read that as such you can get the length or size of the array by using the count property, but if O create an array of Material such as:

var itemMaterials : Material[];
print (itemMaterials.Count);

This gives an error, because Count is not a member of UnityEngine.Material.

array.length

Sorry for not having all this documented. We are working on having all this documented with our next major release.

so this should work??

var itemMaterials : Material[];

function Awake () {
	print (itemMaterials.array.length);

}

itemMaterials is the array, so just itemMaterials.length.

var itemMaterials : Material[];

function Awake () {
	print (itemMaterials.length);
}

reutrns the following error:

‘length’ is not a member of ‘(UnityEngine.Material)’.

Odd, might be a bug - these are all the things you can have there:

Sorry about that, it is an Length instead of length.

var itemMaterials : Material[]; 

function Awake () { 
   print (itemMaterials.Length); 
}