Object Material change with script and a button?

I'm currently using this code so far :

//Change renderer's material each changeInterval
// seconds from the material array defined in the inspector.
var materials : Material[]; 
var changeInterval = 0.33; 
function Update () { 
if (materials.Length == 0) // do nothing if no materials
return; 
// material index 
var index : int = Time.time / changeInterval; 
// take a modulo with materials count so that animation repeats 
index = index % materials.Length; 
// assign it to the renderer
renderer.sharedMaterial = materials[index]; 
}

( Found from Unity Script Reference Help )

and it's working for switching the materials on my plane by a timer. I would like to know if there is a way to make this better by making the materials switch with a button such as a Mouse click or just a button press. Even better would be to figure out a way to click on a made button in the game for the "next material" button.

The furthest I figure is somehow making a counter.

Please help!

i see what you want. ok, here is a script to say that when spacebar is hit to change the material next in the index. if you would like it to be left mouse click then use Fire1 instead of Jump in the script.

var myMaterials : Material[];
var NextMaterial : int = 0;
function NextMaterialUsed()
{
renderer.sharedMaterial = myMaterials[NextMaterial];
if(NextMaterial < myMaterials.length-1)
NextMaterial += 1;
else
NextMaterial = 0;
}

function Update () 
{
if(Input.GetButtonDown("Jump"))
{
NextMaterialUsed();
}

}

hope this help out!

Hi
I hope this may help

How can i use the script to change the material of only one object. I have repeated the same object many times in scene, and pushing the button makes all the objects to change material.