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.