Changing the colour of the cube randomly

How to change the colour of the cube like in the stack Game .

It depends on what Shader you’re using. To change the color of a gameObject when using the Standard Shader:

Color someColor=......
someGameObject.GetComponent<Renderer>().material.SetColor ("_Color",  someColor);

just to add to above, every time you alter .material, it creates a new instance of the shader, which can stack up if you’re not careful. for your case you probably want to create just one during object creation and alter it with .sharedMaterial, and if possible reuse materials from destroyed blocks so you don’t have a billion clogging up memory.

No.
Accessing .material will create a new instance only the first time. Subsequent usages of .material will keep using the same instance that was created the first time.
You don’t have to alter the material for this to happen, you just have to access it.

.sharedMaterial will permanently alter the material in the asset folder.

thanks, i was wrong in my understanding

As i am interpolating the value of the colour list but it is giving always the same shades

void CreateTile()
{
GameObject previousTile = stack[stack.Count - 1];
GameObject activeTile;

activeTile = Instantiate(tile);
stack.Add(activeTile);

if (stack.Count > 2)
activeTile.transform.localScale = previousTile.transform.localScale;

activeTile.transform.position = new Vector3(previousTile.transform.position.x,
previousTile.transform.position.y + previousTile.transform.localScale.y, previousTile.transform.position.z);

// Calculate interpolation factor
float t = (stack.Count - 1) % changeShadeLength / (float)changeShadeLength;

// Determine color indices for interpolation
int colorStep = (stack.Count - 1) / changeShadeLength;
int index1 = colorStep % tileColours.Length;
int index2 = (colorStep + 1) % tileColours.Length;

// Interpolate between the selected colors
Color clr1 = tileColours[index1];
Color clr2 = tileColours[index2];
Color interpolatedColor = Color.Lerp(clr1, clr2, t);

if (!isFirstCall)
{
bottomTile.GetComponent().material.SetColor(“_BaseColor”, interpolatedColor);
isFirstCall = true;
}

activeTile.GetComponent().material.SetColor(“_BaseColor”, interpolatedColor);
activeTile.GetComponent().moveX = stack.Count % 2 == 0;
}

Add some Debug.Log to your code to print out your values