Change materials/textures on mouse click.

Hi, everyone! I understand that this question has been asked quite a few times here and I’ve been reading question-answers for 3-4 hours by now, trying to understand how to do it, but understood nothing. I’m not a coder/programmer so it’s very difficult for me to understand scripting. Now I’m working on an interior realtime visualization project and I need to change materials/textures on the models on mouse click. For instance, a succesive clicking on a wooden table changes its matreial from dark wood to red wood then to another king of wood and then cycle again from the first material. I need to do it for a number of objects in the scene. How can I do it? I’ve looked up character custumization project on Unity site, but it just works and I dont understand how.Maybe, there is a free script for this I can download or anyone may share such a script. If not, I’d appresiate any advice on reading to learn how to do it.
Thanks in advance!

Here’s how. Just use an array of materials and cycle through them with each successive mouse click. Then apply your newly chosen material to your mesh and your good! So like this:

var matArray : Material[];
private var index : int;

function Update () {
	if (Input.GetButtonDown("Fire1")) {
		index++;
		index = index % matArray.Length;
		renderer.material = matArray[index];
	}
}

Add your materials to matArray and ensure this script is attached to the mesh you want to affect. :slight_smile:

Hope that helps, Klep

Are you talking about doing this in the editor, or during play?

In any case, you can use the InputManager to set up an event that your application can listen for in a script that has access to the material for the game object you want modified. You can get the game object by using any of many “picking” techniques based on casting a ray in your scene and determining which object is hit. Once you have the object, you can access the material using its renderer component, and change it.

int nResourceNum = 0;//Use this to cycle through various materials

code to determine what the resource num should be…

Material SomeMaterial = (Material)Resources.Load(string.Format(“Resource{0}”, nResourceNum));
GameObject.renderer.material = SomeMaterial;

Here is how you could do that on right-click:
Of course you need to have a collider attached to the object.

private var originalMaterials : Material[];//all the materials of the object 
var materialIndex: int;//the material you want to toggle
var additionalTextures: Texture[];//the replacement textures
private var Textures: Texture[];//all cycle textures
private var currentIndex: int = 0;
private var n: int;

function Start () {
originalMaterials = renderer.materials;
n = additionalTextures.Length;
Textures = new Texture[n+1];
Textures[0]=originalMaterials[materialIndex].mainTexture;
for (var i=0; i<n; i++) {
	Textures[i+1]=additionalTextures*;*
  • }*

function OnMouseOver () {
if(Input.GetMouseButtonDown(1)){
currentIndex = (currentIndex + 1)%(n+1);

  •  originalMaterials[materialIndex].mainTexture = Textures[currentIndex];*
    

}
}

All you need is to attach this script to the object together with replacement textures and point the materialIndex of the material that needs to be toggled.
And [here][1] you could see this script working. And similar more advanced script [here][2]
[1]: virtualPlayground | 3d technology, walkthroughs, presentations, games
[2]: virtualPlayground | 3d technology, walkthroughs, presentations, games

You can try this, too:

var a : int = 0;
function Start(){
  renderer.material = //material1;
}

function OnMouseClick(){
  a++;
  if(a%2 == 0)
  {
    renderer.material = //material2;
  }
  if(a%2 == 1)
  {
    renderer.material = //material3;
  }
}