- Home /
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?
Answer by Simple · Feb 03, 2011 at 01:33 AM
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
array is flooded via inspector, everything is ok. and it will be not changed in gameplay (there are 66 colors + shaders, cant beleve anyone need more lol).
on another hande, if ill make every case it will be rly big bunch of useless code, thats y i manage do it like that:
materialSelectionGridInt sending int, i get it to use as a number of "case" and number of choosed material
materialsRepresentation[materialSelectionGridInt] = $$anonymous$$aterialOfElement
but, anyway, tyvm ill try to do it
Answer by DEtH_MoroZ · Feb 08, 2011 at 03:24 PM
Problem was here:
materialsRepresentation[materialSelectionGridInt] = MaterialOfElement;
must be:
MaterialOfElement = materialsRepresentation[materialSelectionGridInt];
Your answer
![](https://koobas.hobune.stream/wayback/20220613102528im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to define surface properties on a 2d sprite? 1 Answer
EXC_BAD_ACCESS from Unity::Material::CreateMaterial When running simple app on iPhone. 3 Answers
Replacing sharedMaterials by an existing Material C# 1 Answer
modifying renderer.materials resets scaling of renderer.bounds until transform.position is updated 0 Answers