Why Cant I get different color for instantiated image?

I am Instantiating an image in scroll view containing Grid view.Now when I instantiate image I want to change colours for each image with different colours.The codes I tried are given below.

 Color cc;
void Update()
    {
      
        cc = UnityEngine.Random.ColorHSV();
    }


for (int i = 0; i < infos.Length; i++)
        {
          
            GameObject go = Instantiate(LoadPrefab);
            go.transform.SetParent(prefabParent);

           //I have tried both the below codes but no effect(same colours for different images)

             go.GetComponent<Image>().material.color = cc;
                                 //OR
              go.GetComponent<Image>().color = cc;
                               //OR
          prefabParent.GetChild(i).GetComponent<Image>().color = cc;
}

What render pipeline and shader are you using?

Material.color is a wrapper for the ā€œ_Colorā€ shader property, but some shaders using different color properly names, like URP using ā€œ_BaseColorā€ instead. Why that is, is beyond me.

1 Like

Hi @zyonneo
I’m assuming you are using th standard render pipeline because you didn’t mention which one you are using.

If you want to have different colors using same material, you need to either create new instances of the material or use a material property block (that requires you to customize your shader though.)

Doesn’t changing the color of a material via code create a new instance?
To change the matrial color for all objection you need to use .sharedMaterial.

I am using Standard shader.I am looking to change through code with random values.
So I have to create that much materials.

If it’s a standard ui Image, you can change the color with .color that should work. Unfortunately I see two issues. The constant random color creation in Update and your For loop is just hanging out there. So the question is, where is that for loop really and how is it being called?

ColorHSV may not work on the Image color however. I’ve honestly not tried. I usually just use a standard color and apply that.

1 Like

I understand like this because for loop gets runs quickly than the Update I am getting the same colour right?I think would require to call an array of colours and select accordingly to ā€˜i’ value in the for loop.

I just don’t see why you’d want to get a different color every frame. If you are calling the for loop in another method, why not generate a new color before the for loop? Otherwise, as I said, how are you calling your for loop?

Instead of using the Random function I am trying to get random color using the Update function(bad coding I know).I am using the above for loop to instantiate prefab( which is a Image) but I want different colors in the background.I have given the below code inside the for loop but once when i got same color for background images by chance, now when I check it is showing diff colors.

 for (int i = 0; i < infos.Length; i++)
        {
         
            GameObject go = Instantiate(LoadPrefab);
            go.transform.SetParent(prefabParent);
       
            go.GetComponent<Image>().color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
         }

So you do or don’t have it working now?

yes…any other better way…?

If you want completely random colors, then nothing wrong with what you are doing. If you want random, but you don’t want say two shades of blue that are close to each other, then just make a list of colors yourself and have it randomly select a color. If a color gets selected, remove it from the list so it can’t be selected again.

2 Likes