Change object texture on tap on UI Object

Somebody help me please!
I have code:

    var matArray : Material[];
     private var index : int;
     function Update () {
          if (Input.GetButtonDown("Fire1")) {
            index = index % matArray.Length;
            GetComponent.<Renderer>().material = matArray[index];

But I want change texture only then tap on UI object. How I can do it?


Hello @Zelentsov , If I understand correctly then you want to change the texture of materials when you touch/click on any UI object. For this do the following.

1.Create an empty Gameobject and attach a newly created c# script and paste the following code snippets on that script.

public Material objectMaterial; // material which texture is going to change
public Texture texture2change; // texture that will be reflected on material after change

public void buttonClicked(){
	objectMaterial.mainTexture = texture2change;
  1. Attach the material you want to change and the texture that will be reflected on material after change to the public field of the script.
  2. Create a new UI button/object , select that object on the hierarchy panel , on the inspector tab click ->Add Component → UI → Button(this step is necessary other that Button UI object).
  3. On the Onclick segment of button component, hit ‘+’ sign , Add the newly created Gameobject(not the UI object , the other one) to the field indicating “None(Object)” , click “no function” section , select the script attached with the gameobject and then select the public method buttonClicked().
  4. Save and Run.

You have to attach the material to some gameobject in order to view the reflected change.
Let me know if that helps you.