Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Not sure if it's a Unity bug, or a mistake in my code.

Discussion in 'Scripting' started by SamTyurenkov, Apr 29, 2021.

  1. SamTyurenkov

    SamTyurenkov

    Joined:
    May 12, 2018
    Posts:
    83
    I need to set a Color of UI Image Component in Unity 2020.3.
    Here is my code:

    Code (CSharp):
    1. chassis.GetComponent<Graphic>().color = LvlToColor(DBU.dbu.lvl_chassis);
    What I see in Editor, is that color pallete is picking the color, but the color bar and graphics in Scene/Game view mode remain without change:

     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Shouldn't it be chassis.GetComponent<Image>() ?
     
  3. SamTyurenkov

    SamTyurenkov

    Joined:
    May 12, 2018
    Posts:
    83
    It works either way.

    I found my mistake, it was in a LvlToColor() function.
    I was creating new Color with RGB 0-255, instead of RGB 0-1.
     
    Joe-Censored likes this.
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    To note, Image inherits from Graphic. Graphic provides the color part. You should use Image though, if you plan to use the Image specific stuff. An Image is a Graphic, but a Graphic is not necessarily an Image.
     
    SamTyurenkov likes this.