- Home /
clicking to change material click again to change back
I'm looking for a way to change the material of a 3d object when it is clicked, then changes back to the original material when clicked again.
Answer by Ziad · Aug 16, 2011 at 05:39 PM
u can use this c# script..... name it
ChangeMaterial.cs
using UnityEngine;
using System.Collections;
public class ChangeMaterial : MonoBehaviour {
// put the first material here.
public Material material1;
// put the second material here.
public Material material2;
bool FirstMaterial = true;
bool SecondndMaterial = false;
void Start ()
{
renderer.material = material1;
}
void OnMouseDown()
{
if (FirstMaterial)
{
renderer.material = material2;
SecondndMaterial = true;
FirstMaterial = false;
}
else if (SecondndMaterial)
{
renderer.material = material1;
FirstMaterial = true;
SecondndMaterial = false;
}
}
}
Answer by DaveA · Aug 16, 2011 at 05:29 PM
Look no further than the wonderful online documentation itself: http://unity3d.com/support/documentation/ScriptReference/Material.html http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDown.html
Read it before posting, got the material to change on mouse down, but changing back to the original is my problem. Should of been more clear sorry.
Answer by Leonidax · Aug 16, 2011 at 10:04 PM
Everything is in the documentation:
To change the texture first get the GameObject to get the current texture, get its Transform and then its Renderer get the Renderer's Material and finaly Get the Texture from the Material, I don't know how are you planing to load the other texture but at the end you only Set the new Material's Texture.