- Home /
Material Selection problem for Rendering
Hi I am new to unity3d. i am just trying to change the Material of wall at run time.... So i just import the model of house form Google Sketch up.... Which have different material in one object...As you can see in inspector view......
my problem is that when ever i click the next button ">>" it change the first material of the object....As shown in image, Red circle in inspector view is representing that the first material is changing and in game view there is next button under the red circle....
Some how if i get the ref of other elements then that will be great for me.... Code is given below....
public class Material_GUI : MonoBehaviour {
public Material[] mats; public GameObject go; private int index = 0; // Use this for initialization void Start () { go.renderer.material= mats[index]; } // Update is called once per frame void Update () { }//end of if update
void OnGUI(){ GUILayout.BeginArea(new Rect(Screen.width/2-100,Screen.height-60,200,50)); GUI.Box (new Rect(10,10,190,40),""); GUI.Label(new Rect(62,20,100,20),"Wall Testing"+(index +1)); if(GUI.Button (new Rect(15,15,30,30),"<<")){ index--; if(index<0){ index = mats.Length - 1; } go.renderer.material = mats[index]; } if(GUI.Button (new Rect(165,15,30,30),">>")){ index++; if(index > mats.Length -1){ index = 0; } go.renderer.material = mats[index]; } GUILayout.EndArea(); }//end of OnGUI Funcation
}
Answer by Maulik2208 · Dec 10, 2012 at 10:38 AM
Can you explain what are you upto???? Beacuse according to me if you want to change Texture then you will need some kind of modification in your script so please provide your feed back what do you want???? For changing the look of the wall you can simply change the texture and you are all set to go and if you are trying to do the same then just use Gameobject.renderer.material.Maintexture = texturename; and Enjoy........For more info please refer to this link link text
Thanks man... But my problem is that there are different room in my house model and 15 materials of texture are there which is denoted by element 0 to element 14 as shown in image....
each element is representing texture(material)of different room.... i am using the above code for rendering... it is just changing my first element material.....which is located at element 0,,,,,my entire model is in mesh form.....
so my problem is that if some how i am able to get 2 or 3 element texture in code.......
so it will great for me.... i want to change the marital(texture) of the walls at run time....
i want to know how i can assess the element 2 or 3 at run time... by scripting....
Thank you...!!!
Answer by Hardcore Games · Aug 15, 2014 at 03:23 PM
Well my friend. I have the same problem you have. So far I can tell you this:
if (GUI.Button(new Rect(120,250,200,40),'Ladrillos Crudos')){
theObject = GameObject.Find(casterScript.theThing);
mats = theObject.renderer.materials;
for (mat = 0 ; mat < theObject.renderer.materials.Length -1 ; mat++){
mats[mat] = materialPared_3;
}
theObject.Renderer.materials = mats;
/I've Also tryied theObject.cachedRenderer.materials = mats; with no change /
}
As you can figure out, the GUI.Button will trigger the material changing. casterScript is a RaycastHit returning the name of the mesh and the type of material stored in its Tag. What I've read is that you can only change different elements materials by refering to an array called "materialS". Then you'll need to go though this array changing the right element in the already declared material array (Declare lik this: var mats : Material[];) This code hasnt work for me so far. I hope this help you in your research. Please, let me know I you found final solution. Thnx
Your answer
