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

Resolved Cannot change Image color [2020.2.1f1 Personal]

Discussion in 'Scripting' started by NOT_A_ROBOT1101, Jan 13, 2021.

  1. NOT_A_ROBOT1101

    NOT_A_ROBOT1101

    Joined:
    Jun 18, 2020
    Posts:
    39
    I cannot seem to change the color of an Image now. First I thought it's because I hadn't assigned it a sprite, but after assigning it a sprite, it still didn't work.

    I took a look at the documentation of Color and it said it expects the value to be between 0f and 1f, so I changed it to that and it still didn't work.
    Looking at one of the answers in Unity Answers, someone said that using Color32 solves the problem. It didn't. (Yes, I did put the RGBA values of Color32 between 0 and 255.)

    Then I found a bug in Unity Issue Tracker that says it only happens if your image type is "Simple", so I modified the sprite a bit and set the image size to "Sliced". (which doesn't give any warning/errors in the inspector/console)

    It didn't work.

    I found another post in Unity Answers that said that it is because the sprite is black.
    The sprite I made, however, was a completely white 1x1px square. (and I set the color in the inspector to be dark blue)

    - Yes, the inspector does modify the color of the image.
    - No, the code doesn't give an exception.
    - Yes, I made sure the color values are in the expected range (0f to 1f for Color, 0 to 255 for Color32)
    - No, the beginning color of the image wasn't white, it was dark blue and I tried to change that color to white using a script.
    - No, I didn't accidentally drag and drop the wrong image.
    - Unity version: 2020.2.1f1 Personal <DX11>
    - Windows OS: Windows 10 64-bit
    - CPU: 4-core @ 3.5GHz
    - RAM: 4GB

    Can anyone help me?

    Thanks in advance!

    (The important part of the) code:
    Code (CSharp):
    1. [SerializeField]
    2. private Image targetImage;
    3.  
    4. public void Update() {
    5.     targetImage.color = Color.white;
    6. }
     
    Last edited: Jan 13, 2021
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Sounds like you've done a good job of checking for all the most common problems.

    I suggest that you add a Debug.Log statement to your script to verify that it's running at all. While you're at it, maybe make it print the value of targetImage.color before you change it; see if the value is what you expect.

    Is there a Button component involved at all? Buttons that use color-tint transitions (which are the default) will mess with the color of their target Image in order to show the button's state. (Which wouldn't necessarily cause your problem, but would be another thing to check.)

    Is there a prefab involved at all? Sometimes people accidentally use a reference to a prefab asset instead of to an instance of that prefab in their scene, or things like that.
     
  3. NOT_A_ROBOT1101

    NOT_A_ROBOT1101

    Joined:
    Jun 18, 2020
    Posts:
    39
    oh wow I can't believe I misclicked on the checkmark of the script, no wonder it's not running
    I'm really sorry.