Changing colour of player

Sorry for the incredibly simple question, but I’m new to Unity. I’m trying to write code which changes the colour of the player (this is a prototype so it’s just a simple 3D capsule) when the player presses a button.
First, I created two variables to keep track of the current colour and store all three colours.

public int currentColour;     
public Material[] colours;

Then, in the “void Update()” command, I tried

     { if (Input.GetButtonDown("ColourForward"))
      {  GameObject capsule = gameObject.transform.Find("Capsule").gameObject;
        capsule.GetComponent<MeshRenderer>().materials[0] = colours[2]; }}

The capsule mesh is a child of the object that the code is attached to. Pressing the button definitely does something because the material on the capsule changes from “Green Material” to “Green Material (Instance)” when the button is pressed.

Sorry if this is really simple, but any help would be appreciated.

You’re on the right path.
Here’s some help:

private int currentMaterial; // keep things private unless you need to access them from other components
[SerializedField] private Material [] materials; // mark them as Serialized if you want them in the Editor

[SerializedField] private Renderer renderer; // keep a reference to the object's renderer, mark it Serializable and assign it in the inspector

void Awake ()
//    renderer = gameObject.transform.Find("Capsule").GetComponent<Renderer>(); // or assign it in awake

void Update ()
    if (Input.GetButtonUp ("ColourForward") // GetButtonUp usually provides a better behaviour
        SwitchMaterial (1); // use another method to make things a little more generic

void SwitchMaterial (int index)
    renderer.materials[0] = materials[index]; // use this if you have more than one material on the mesh
//    renderer.material = materials[index]; // otherwise, this will do the job