Search Unity

UI Image color not updating correctly

Discussion in 'UGUI & TextMesh Pro' started by Rafarel, May 8, 2019.

  1. Rafarel

    Rafarel

    Joined:
    Jul 21, 2017
    Posts:
    199
    Hello, I'm using Unity UI Image to display a preview of my level.
    I change it's color from script to change it's color alpha with two colors I have set in the inspector.
    I have done this a lot and it's should be working.
    The code is very easy :

    Code (CSharp):
    1. public Image Preview;
    2. public Color ValidColor;
    3. public Color InvalidColor;
    4.  
    5. ...
    6.  
    7.  
    8. Preview.color = m_Playable ? ValidColor : InvalidColor;
    Even if I change the color of the Image during run time through the inspector, nothing changes on the game view.

    This bug happen only on this particular Image class, I've never seen that before.
     
    ActuallyAscyt and greatastrocow like this.
  2. Rafarel

    Rafarel

    Joined:
    Jul 21, 2017
    Posts:
    199
    Here is a screenshot of my image and it's color is set to red through the inspector but it does not change at all ...

    Unity-image-color-bug.PNG
     
  3. Rafarel

    Rafarel

    Joined:
    Jul 21, 2017
    Posts:
    199
    Anyone can help me with this please :) ?
     
  4. motorious

    motorious

    Joined:
    Jun 25, 2016
    Posts:
    11
  5. Rafarel

    Rafarel

    Joined:
    Jul 21, 2017
    Posts:
    199
  6. LiveOrDevTrying

    LiveOrDevTrying

    Joined:
    Nov 27, 2016
    Posts:
    2
    Thank you for this response - this behaviour suddently was broken in my game and I could not figure it out. I unfortunately do not have time for unity to figure out how to fix this, so instead I changed my Image Type to Filled (fill amount 1) instead of Simple and the problem was solved.

    Thank you again!
     
    greatastrocow likes this.
  7. Rafarel

    Rafarel

    Joined:
    Jul 21, 2017
    Posts:
    199
    The issue is fixed on Unity 2019.1.8 :)
     
  8. kaliAJ

    kaliAJ

    Joined:
    Sep 29, 2018
    Posts:
    18
    @Rafarel @LiveOrDevTrying

    hey, I am using 2019.1.9f1 of unity still I am facing these issue.
    basically I need to change the colors of the scoreboard in my scene, I can change colors for 1 scoreboard (which is basically is an image) but not for the other one, which is exactly the same as the previous.

    btw both have the image type as sliced.
     
    ActuallyAscyt likes this.
  9. mario_code4ever

    mario_code4ever

    Joined:
    Jan 9, 2020
    Posts:
    36
    Version 2019.2.17f1 got issue same trying to change color on my pause panel it's Screenshot_1.png not working.
     
  10. ManieBesselaar

    ManieBesselaar

    Joined:
    Nov 1, 2018
    Posts:
    30
    Maybe this will help someone else in future. I wanted to change sprite color from code. found that using Color had no effect but Color32 worked great.
    Hope this helps.
     
  11. TheCoone

    TheCoone

    Joined:
    Apr 4, 2020
    Posts:
    1
    For me the solution was quite simple. The alpha was simply set to 0 for some reason. So change the alpha of the new color to 255.
     
  12. A_Full

    A_Full

    Joined:
    Mar 7, 2019
    Posts:
    1
    I had the same problem.
    In script the range color value is 0->1 and not 0->255.
     
    Durium, ishu1999, Randunel and 2 others like this.
  13. magg

    magg

    Joined:
    Sep 29, 2013
    Posts:
    74
    Can confirm - this worked for me 2019.3.14f1.
    Spent 2 hours trying to figure out what I do wrong.

    Edit: yeah, only works once. If the color needs to be changed more than once, it doesn't work.
     
    Last edited: Jun 7, 2020
  14. eyeseemint

    eyeseemint

    Joined:
    Jul 20, 2020
    Posts:
    1
    Thanks. Sounds obvious but I think I just naturally assumed that the Color property set via mono behavior would be the same default alpha value set by unity. I'm ashamed to admit I have spent way too much time on this lol
     
  15. billykatz

    billykatz

    Joined:
    Apr 2, 2021
    Posts:
    5
    Like @A_Full said, This is what worked for me.

    Code (CSharp):
    1. private Color lowRed = new Color(255.0f / 255.0f, 117.0f / 255.0f, 117.0f / 255.0f);
     
    Qmansvednetta likes this.
  16. Kinnith7

    Kinnith7

    Joined:
    Jul 4, 2017
    Posts:
    84
    Thanks! I just got hit with it in 2021.1.21f1. And this fixed it. Thanks for posting this fix. Was driving me crazy.
     
    ManieBesselaar likes this.
  17. White_Wabbit

    White_Wabbit

    Joined:
    Aug 16, 2019
    Posts:
    11

    If you click on the colour picker in Editor, you can actually see that there is 3 options
    1. Color32
    2. Color
    3. HSV
    Which you can see the range of each type.
     
  18. ActuallyAscyt

    ActuallyAscyt

    Joined:
    Sep 11, 2021
    Posts:
    9
    I tried everything mentioned here, and it still doesn't work. Any help would be appreciated
     
  19. kira911911

    kira911911

    Joined:
    May 29, 2015
    Posts:
    10
    That fixed for me. Thank you!
     
  20. kira911911

    kira911911

    Joined:
    May 29, 2015
    Posts:
    10
    Have you tried the color32 instead of color?
     
  21. Tentakero

    Tentakero

    Joined:
    Mar 5, 2017
    Posts:
    17
    Anyone have a fix for this besides the Color32 thing? Doesn't seem to be working for me.
     
  22. Qmansvednetta

    Qmansvednetta

    Joined:
    Mar 8, 2017
    Posts:
    8
    Fixed my problem! Thanks!
     
  23. gooby429

    gooby429

    Joined:
    Aug 13, 2019
    Posts:
    135
    why is this still an issue and there are no solutions online lol
     
  24. BunchOfJunk

    BunchOfJunk

    Joined:
    Dec 10, 2017
    Posts:
    2
    I have the same/similar problem. Trying to change the attributes of a menu button in a script attached to the button (see below). Nothing on the screen or in the inspector changes. Have tried suggestions above but they don't work for me either. Used debug to make sure I have the correct game object. Was able to enable/disable the button via code just not the attributes.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class Setup_Button : MonoBehaviour
    {
    private ColorBlock colorBlock;

    void Awake()
    {
    colorBlock = gameObject.GetComponent<Button>().colors;
    }
    void Start()
    {
    colorBlock.normalColor = Color.cyan;
    colorBlock.fadeDuration = 0.5f;
    }
    }
     
  25. BunchOfJunk

    BunchOfJunk

    Joined:
    Dec 10, 2017
    Posts:
    2
    Found the answer. Need to reassign the color block back to the object.

    private Button button;
    private ColorBlock colorBlock;

    void Start()
    {
    button = gameObject.GetComponent<Button>();
    colorBlock = button.colors;
    colorBlock.normalColor = Color.cyan;
    colorBlock.fadeDuration = 0.5f;
    button.colors = colorBlock;
    }
     
  26. talha-safdar

    talha-safdar

    Joined:
    May 28, 2022
    Posts:
    7
    The only SOLUTION. Thank you man!
     
  27. Drunal

    Drunal

    Joined:
    Jan 6, 2022
    Posts:
    1
    I just stumbled accross the same issue.
    In my case it was simply that the "colorpicker" automatically set the Alpha to "0" (why so ever).
    So the Inspector showed me the colors changing, but nothing happend on the game view -> because Alpha was 0