Cycle through object materials on mouse click in C#

I have an object in my scene - a projection screen - that I need to cycle through different “slides” on a mouse click. I’ve tried working with a bunch of different examples I’ve come across, but it seems like the API update with Unity 5 (combined with my lack of experience) is throwing me for a loop. The chunk of code I’ve come up with seems like it should work, but when I run the game, I get bupkis other than the default material.

using UnityEngine;
using System.Collections;

public class SwapTextures : MonoBehaviour
{
	public Material[] myMaterials;
	int arrayPos = 0;
	
	void updateMaterials ()
	{
		if (Input.GetMouseButtonDown (0)) {
			arrayPos++;
			arrayPos %= myMaterials.Length;
			GetComponent<Renderer> ().material = myMaterials [arrayPos];
		}
	}
	
	void Update ()
	{
		updateMaterials ();
	}
}

I see you’re using the same material multiple times within your ‘MeshRenderer’ component; more than one material should only be used here if your model requires multiple materials for rendering. There is no point iterating through the materials list if all materials are the same. Are you only changing one material, or will you eventually be using multiple materials for the ‘Mesh1’ model?

If you are only changing a single material, I would use your original code which works perfectly fine. However I would suggest using ‘sharedMaterial’ instead of ‘material’.

If you’re planning on changing all the materials, remember that all arrays in Unity return a copy, meaning that if you’d like to change the materials array you’ll need to get a copy of the materials array, edit the copy, and then set the materials back to the object.

Here are a couple examples of what you could do:

  1. If you’re changing multiple materials:

    using UnityEngine;

    public class SwapTextures : MonoBehaviour
    {
    public Material MyMaterials;

     private int arrayPos;
     private Material[] modelMaterials;
    
     void Start()
     {
         modelMaterials = GetComponent<MeshRenderer>().sharedMaterials;
     }
    
     private void UpdateMaterials()
     {
         if(Input.GetMouseButtonDown(0) && MyMaterials.Length > 0)
         {
             arrayPos++;
             arrayPos %= MyMaterials.Length;
    
             // If changing multiple materials...
             for(var i = 0; i < modelMaterials.Length; i++)
             {
                 modelMaterials *= MyMaterials[arrayPos];*
    

}
GetComponent().sharedMaterials = modelMaterials;
}
}

void Update()
{
UpdateMaterials();
}
}

2) If you’re changing a single material:
using UnityEngine;

public class SwapTextures : MonoBehaviour
{
public Material[] MyMaterials;

private int arrayPos;

private void UpdateMaterials()
{
if(Input.GetMouseButtonDown(0) && MyMaterials.Length > 0)
{
arrayPos++;
arrayPos %= MyMaterials.Length;
GetComponent().sharedMaterial = MyMaterials[arrayPos];
}
}

void Update()
{
UpdateMaterials();
}
}