Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why Cant I get different color for instantiated image?

Discussion in 'Scripting' started by zyonneo, Dec 18, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    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.
    Code (CSharp):
    1.  Color cc;
    2. void Update()
    3.     {
    4.      
    5.         cc = UnityEngine.Random.ColorHSV();
    6.     }
    7.  
    8.  
    9. for (int i = 0; i < infos.Length; i++)
    10.         {
    11.          
    12.             GameObject go = Instantiate(LoadPrefab);
    13.             go.transform.SetParent(prefabParent);
    14.  
    15.            //I have tried both the below codes but no effect(same colours for different images)
    16.  
    17.              go.GetComponent<Image>().material.color = cc;
    18.                                  //OR
    19.               go.GetComponent<Image>().color = cc;
    20.                                //OR
    21.           prefabParent.GetChild(i).GetComponent<Image>().color = cc;
    22. }
     
  2. doarp

    doarp

    Joined:
    Sep 24, 2019
    Posts:
    147
    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.
     
    noio likes this.
  3. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    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.)
     
  4. doarp

    doarp

    Joined:
    Sep 24, 2019
    Posts:
    147
    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.
     
  5. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    I am using Standard shader.I am looking to change through code with random values.
    So I have to create that much materials.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    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.
     
    Last edited: Dec 18, 2019
    zyonneo likes this.
  7. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    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.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    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?
     
  9. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    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.
    Code (CSharp):
    1.  
    2.  for (int i = 0; i < infos.Length; i++)
    3.         {
    4.          
    5.             GameObject go = Instantiate(LoadPrefab);
    6.             go.transform.SetParent(prefabParent);
    7.        
    8.             go.GetComponent<Image>().color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
    9.          }
     
    Last edited: Dec 19, 2019
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    So you do or don't have it working now?
     
  11. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    yes..any other better way..?
     
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    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.
     
    zyonneo and Joe-Censored like this.