Q:

Change the color of instantited prefabs

i want to change the color of instantiated prefabs 
i was able to get to this point but the instantiated prefabs color stays the same , but the materiel colors changed 
here is the code 
public class InstantiatePrefabs : MonoBehaviour
{
    public GameObject cube;
    Renderer rend;

    Color[] colors = new Color[6];
    // Start is called before the first frame update
    void Start()
    {
        rend = GetComponent<Renderer>();
        InvokeRepeating("Spawn", 0, 3);

    }

    public void Spawn()
    {

        float x = Random.Range(-2, 2);

        GameObject newCube = (GameObject)Instantiate(cube) as GameObject;
        newCube.transform.position = new Vector2(x,6);
        int randcol = Random.Range(1, 6);
        if (randcol == 1)
        {
            rend.material.color = Color.red;

            // print("red");
        }
        if (randcol == 2)
        {
            //  print("green");
            rend.material.color = Color.green;
        }
        if (randcol == 3)
        {
            rend.material.color = Color.blue;
        }
        if (randcol == 4)
        {
            rend.material.color = Color.yellow;
        }

    }

A:

Your issue is that you were gettiing the Renderer of the GameObject instantiating the Cubes not the Renderer of the Cubes.
public class InstantiatePrefabs : MonoBehaviour
{
    public GameObject cube;
    public Color[] colors;

    void Start()
    {
        InvokeRepeating("Spawn", 0, 3);
    }

    public void Spawn()
    {
        float x = Random.Range(-2, 2);

        GameObject newCube = (GameObject)Instantiate(cube) as GameObject;
        newCube.transform.position = new Vector2(x,6);

        // Get the Cubes Renderer
        Renderer rend = newCube.GetComponent<Renderer>();

        //Select a random Color from the Colors set in the inspector
        //Making this dynamic you can change the colors and this will select a random color within the range of colors.
        Color randcol = colors[Random.Range(0, colors.Length)]

        //Set color to the cubes Renderer
        rend.material.color = randcol;

         //OR use the if statements like you were (not the best way!)
        /*
        int rand = Random.Range(1, 5)
        if (rand == 1)
        {
            rend.material.color = Color.red;
            // print("red");
        }
        if (rand == 2)
        {
            //  print("green");
            rend.material.color = Color.green;
        }
        if (rand == 3)
        {
            rend.material.color = Color.blue;
        }
        if (rand == 4)
        {
            rend.material.color = Color.yellow;
        }
        */
    }
}

If you have any questions about my changes don't hesitate to ask.

