Beginner: Instantiate cubes and then modify them

Hi all,

First post here.
I’ve recently started learning Unity and in need of some help.

I have a couple questions and was hoping someone could please help me out

Two questions: In this code, why do all the cubes change colour and the one in the bottom left is always a lighter colour? How would I go about assigning a different random colour to each of the cubes? I know if I use the update function I can do this, but I would like to leave it in a separate function.

Second question is how can I differentiate between the 36 cubes I created? Do I need to give them tags (cube1, cube2 etc). I would like to later give them various commands. For example, pressing a button would cause a specific cube (say cube16) to change colour.

	void Spawn(){

		int x = 6, y = 6, count = 1;
		for(int i = 1; 7 > i; i++)
		{
			x = i;
			for(int j = 1; 7 > j; j++)
			{
				y = j;
			Instantiate(cube,new Vector3(x,y,1),Quaternion.identity);

				int k = Random.Range(0,2);
				if (k == 1){
				cube.renderer.material.color = Color.red;
				}else{
					cube.renderer.material.color = Color.blue;
				}

			Debug.Log("Creating cube number: " + count+" "+k);
				count = count +1;
			}
		}
	}

Cheers,
Grish

You’re changing the colour of cube, which is the prefab from which your other cubes are copied. You need to change the colour of the individual cube you just instantiated. I.e.

GameObject thisCube = Instantiate(cube,new Vector3(x,y,1),Quaternion.identity);
...
thisCube.renderer.material.color = Color.red;
...