Changing colors using the same button

I’m working on an assignment for class. The requirement is to change the colors of the blocks using an array and a button press. Then, using that same button, change them back to the same color they started in. (They start off as white) However, I cannot seem to wrap my head around on how to use the same button press to change the colors back and forth. Below is the code that changes the color and the array with it, Any help would be appreciated. I believe my error has something to do with the Update function, yet, even then, i’m not sure on what the correct fix would be.

using UnityEngine;
using System.Collections;

public class ArrayScript : MonoBehaviour {
	//public Color[] Cubes;

	public GameObject[] components; 
	Color[] originalColor;
	void Start()
	{
		originalColor = new Color[components.Length];
		for (int i = 0; i < components.Length; i++) 
		{
			originalColor  _= components *.GetComponent<Renderer> ().material.color;*_

* }*

* }*
* void Update()*
* {*
* if (Input.GetKeyDown (KeyCode.A))*
* {*
* components[0].GetComponent().Change(Color.black);*
* components[1].GetComponent().Change(Color.green);*
* components[2].GetComponent().Change(Color.red);*
* components[3].GetComponent().Change(Color.grey);*
* components[4].GetComponent().Change(Color.blue);*
* components[5].GetComponent().Change(Color.yellow);*
* }*

* if (Input.GetKeyDown (KeyCode.A))*
* {*
* components[0].GetComponent().Change(Color.white);*
* components[1].GetComponent().Change(Color.white);*
* components[2].GetComponent().Change(Color.white);*
* components[3].GetComponent().Change(Color.white);*
* components[4].GetComponent().Change(Color.white);*
* components[5].GetComponent().Change(Color.white);*
* }*
* }*
}

Hi! First, let’s look at why your code doesn’t work:

  • Every frame (i.e. Update), you check if the A key has gone down
  • If it has, you colour all your Objects
  • Then you check if the A key has gone down (which will give the same result as before – this is all in the same frame, after all)
  • If it has, you set them all white

So, every frame, if you press the A key, you set everything to a colour then set everything white again. (And this is before you even get to rendering!)

What you’re after is a simple toggle, as @Mavina has shown. However, looking at your code, I don’t think you want the hardcoded colours and there’s a couple of simple improvements you could make (no need to use GetComponent every time – if you know that’s what you want, just store it as that!).

Here’s how I would rewrite your code:

using UnityEngine;
using System.Collections;

// I'd consider a better name -- this doesn't really tell you what it's doing!
public class ArrayScript : MonoBehaviour
{
     // If you want the ColorChange component, just use it =)
     // You'll still be able to drag the GameObject on in the inspector
     public ColorChange[] ColorChanges;

     private Color[] _originalColors;
     // If true, should show in color, if false should show in white
     private bool _isColor = false;

     private void Start()
     {
           // I'm leaving the idea of this in, though the way everything's written, it does feel like something ColorChange should be taking care of!
           _originalColors = new Color[ColorChanges.Length];
           for (int i = 0; i < ColorChanges.Length; i++)
           {
                 // Renderer.material creates a copy of the Material, so don't use it if you don't need to/that's actually what you want
                 _originalColors _= ColorChanges*.GetComponent<Renderer>().sharedMaterial.color;*_

}

SetColors();
}

private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
// Toggles the color setting
_isColor = !_isColor;
SetColors();
}
}

private void SetColors()
{
for (int i = 0; i < ColorChanges.Length; i++)
{
// The x ? y : z here is just a shorthand if/else statement for this case
// Use as: (condition) ? (value if true) : (value if false)
ColorChanges.ChangeColor(isColor ? originalColors : Color.white);
}

}
}
Let me know if that works out for you. Excuse any typos – stuck on a tablet at the moment. =)
I would look to moving all the colour setting onto the ColorChange component though, since it looks like that’s what you’ve written it for?