Static variable and Materials

Hi there! im trying to create smt like lego game. Now im trying to create system, thats allow player to choose between several (actually 66) materials for blocks. here is the code: (java)

var boxIconTextures : Texture[];
var materialsRepresentation : Material[];

static var MaterialOfElement: Material;

function MaterialOfElementChange() {

var materialSelectionGridInt : int = 0;

materialSelectionGridInt = GUI.SelectionGrid (Rect (25, 80, 700, 480), materialSelectionGridInt, boxIconTextures, 16);
    switch (materialSelectionGridInt) {
        case (materialSelectionGridInt): materialsRepresentation[materialSelectionGridInt] = MaterialOfElement; print ("material switched"); break;
    }
}

and here is code for element creating:

var BoxTexture;
var check ;
var thePosition ;
var Brick : LayerMask;
static var MaterialOfElement;

function Fire () {

thePosition = transform.TransformPoint(Vector3.forward * 1.5);

check = Physics.CheckSphere (thePosition, 0.3, Brick);

if (check == true)
return;
if (check == false)

var element = GameObject.CreatePrimitive(PrimitiveType.Cube);
element.renderer.material.mainTexture = BoxTexture;
element.layer = 8;
element.renderer.material = CubeGui.MaterialOfElement;
element.gameObject.AddComponent ("Destroy");
element.transform.position = thePosition;
element.gameObject.AddComponent ("SetToGrid");
}

the problem is: im choosing new material from MaterialOfElementChange() but its wont apply on creating objects. i manage that its happen cos of mistakes in MaterialOfElementChange() but everything looks like its work correctly. may be its happen cos static vars not allow to use it for materials? :D (cant believe that)

edit1: ive tryed what Simple says, but it wont work. i find out that when i start script of material change (ongui part) its says "material changed" lol so i gonna check some docs on grids and toolbars.

edit2: forget to say:

materialsRepresentation = new int[10];

equals

Cannot convert 'int[]' to 'UnityEngine.Material[]'.

array is created via inspector, so, somehow grid just dont want to work. maybe its happen cos of another toolbar in same script?

switch (materialSelectionGridInt) { case (materialSelectionGridInt): materialsRepresentation[materialSelectionGridInt] = MaterialOfElement; print ("material switched"); break;

I suppose your switch-case doesnt work. It should be like:

switch (materialSelectionGridInt) {
//if (materialSelectionGridInt==1)
        case 1: materialsRepresentation[1] = Material1; 
print ("material switched"); break;
//if (materialSelectionGridInt==2)
        case 2: materialsRepresentation[2] = Material2; 
print ("material switched"); break;
//etc
//...
}

UPDATE: And I am not sure byt you have created empty and not editable array "materialsRepresentation" because you use BuiltIn array. And you need to add this

materialsRepresentation = new int[10];

Because you cant resize BuiltIn arrays without reinitializing. http://unity3d.com/support/documentation/ScriptReference/Array.html

Problem was here:

materialsRepresentation[materialSelectionGridInt] = MaterialOfElement;

must be:

MaterialOfElement = materialsRepresentation[materialSelectionGridInt];