- Home /
Changing a material with a mouse click
I'm a Unity noob so please bear with me!
I want to be able to click on an object and change its material. I would like to have the option of doing this 2 different ways:
1) Simply repeat clicking on the object will change the material to the next one. 2) Click on the object to display a GUI that I can pick what material I want.
Any ideas how I would approach this? Thanks.
Answer by Matte Szklarz · Oct 26, 2010 at 11:18 AM
I just got on here to find this same thing! But I got an answer:
You need to define an array for materials, place the materials in the array by selecting them in the GUI, and then use a function to change the rendering material. Here is my project that involves the object changing material when it's health hits 1:
var health: int; var materials : Material[];
function Update () { if ( health == 1 ) renderer.sharedMaterial = materials[0]; else renderer.sharedMaterial = materials[1];
}
All you have to do is a write a function that controls the array index via mouse click!
Answer by MrBurns · Nov 19, 2010 at 05:10 PM
Well, here is simple script for GUI button...
var object : GameObject; var texture : Texture;
function OnMouseDown(){ object.renderer.material.mainTexture = texture; }
Your answer
Follow this Question
Related Questions
How do i change default material ? 1 Answer
Change GUI on mouse over 1 Answer
How to change all materials of object and then change it back to normal? 0 Answers
Detect a click outside a GUI/object 4 Answers
Object Movement via Mouse Click? 3 Answers