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.

3 Answers

3

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;
    }
}

}

This solution did not work for me.

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

didn't my answer work?

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.