4

I have a GameObject with a SpriteRenderer and its default color is set to white. At runtime I'm setting the color to Color.gray and for some reason it is not updating at runtime to the new gray tint. I have confirmed that the objects in my script are correct because the sprite completely disappears if I set enabled to false and if I have the object selected in the editor and play, the color changes in the Inspector but does not update in the Game View.

This is my simple script

public class GameManager : MonoBehaviour
{
    public SpriteRenderer Player1;
    public SpriteRenderer Player2;

    public void SetTurn(bool isPlayer1)
    {
        this.Player1.color = Color.gray;
        this.Player2.color = Color.gray;

        if (isPlayer1)
            this.Player1.color = Color.white;
        else
            this.Player2.color = Color.white;
    }
}

SetTurn is being called from a different script in its Update on a mouse click; I have verified that the function is actually running. I've also tried simplifying this to:

public class GameManager : MonoBehaviour
{
    public SpriteRenderer Player1;
    public SpriteRenderer Player2;

    private void Update()
    {
        this.Player1.color = Color.red;
    }
}

That doesn't even work. There are no warnings and no errors in the console either. I've tried rebooting, rebuilding, quitting and restarting, running on a different machine and running on a completely different platform... nothing works.

Any idea what I'm doing wrong? Again, the color changes in the Inspector at runtime but does not in the Game View. Also, if I change the color manually in the Inspector at runtime the Game View does change color, it just refuses to do it from the script.

EDIT

I've also tried using Color32 just to cover my bases and that doesn't work, as I expected it wouldn't.

UPDATE

I completely removed the script and made a new script that I attached directly to the GameObject itself and that doesn't even work.

public class test : MonoBehaviour
{
    private SpriteRenderer m_Renderer;

    void Start()
    {
        this.m_Renderer = this.GetComponent<SpriteRenderer>();
    }

    void Update()
    {
        this.m_Renderer.color = Color.black;
        Debug.Log("COLOR BLACK");
    }
}

The console displays "COLOR BLACK" so I know the Update is being called but still no color change in Game View or Scene View but it does change in the Inspector

UPDATE 2

I narrowed it down but still haven't solved it. I'm using the 2d experimental build 5.5.0a6. I installed the current stable version 5.4.2f2 and the problem does not exist! So, the problem has something to do with the SpriteRenderer in version 5.5.0a6. I'm trying to find out what the issue is, and I hope this gets fixed soon.

UPDATE 3 Here is a link to 2 simple projects that illustrate the issue. One for version 5.5.0a6 which DOES contain the issue and the exact same project for version 5.4.2f2 which DOES NOT contain the issue.

Sample Projects

vane
  • 2,125
  • 1
  • 21
  • 40
  • Have you tried to disable the renderer and enable it right after (after changing the color) ? – Hellium Nov 09 '16 at 08:15
  • @Hellium Just tried it and it still doesn't work. I tried disabling then making the change then enabling and also making the change then disabling and enabling again immediately. – vane Nov 09 '16 at 08:17
  • what material/shader are you using on sprite ? You can try to do Player1.color = new Color (1f,0f,0f,1f); – Demon Nov 09 '16 at 08:18
  • @Demon the `Sprites-Default` material. Again, this works perfectly fine in the editor using the inspector at runtime so I don't think the material would be the issue, I've also tried every other available material provided by unity itself and those don't work either. Doing `new Color(1f, 0f, 0f, 1f)` changes it in the inspector but not the game view, same as everything else I've tried. – vane Nov 09 '16 at 08:20
  • any animations attached? maybe changing the color in the animation – Bizhan Nov 09 '16 at 10:01
  • @Bijan No animator attached but I will try this as a workaround first thing tomorrow. Unfortunately this looks like a bug in the 2D experimental build since it does not exist in the stable build. I wish they would release the 9 slice sprite capabilities soon. – vane Nov 09 '16 at 10:03
  • also try material.color – Bizhan Nov 09 '16 at 10:16
  • Hey can you create a simple project for this problem and share it? I want to take a look at it. – Programmer Nov 09 '16 at 13:48
  • @Programmer I added a link to a zip file containing 2 projects for 2 different version of Unity. One with the issue and one without, both projects are identical. – vane Nov 09 '16 at 18:33
  • @Bijan I tried `material.color` and I also tried `material.SetColor("_Color", xxx)` but they both do not work. – vane Nov 09 '16 at 18:34
  • Ok. Which one is with the issue and which one is not? – Programmer Nov 09 '16 at 18:36
  • @Programmer The update says which is which but for clarity `test 5.5.0a6` DOES have the problem and `test 5.4.2f2` DOES NOT. – vane Nov 09 '16 at 18:39
  • Ok I tried 5.5.0a6 but it did not work because I have 5.4.xx. I get "Failed to load 'Assets/test.unity' because its serialized file version is higher than what this version of Unity supports." error. Is this version made wirh 5.5xxx? If so,sorry I cannot help because I don;t have that version. My suggestion is for you to create create a new project and new script. That script should work fine. Otherwise, it is a bug. – Programmer Nov 09 '16 at 21:36
  • @Programmer yeah, the 5.5.0a6 one was made with version 5.5.0a6 and the 5.4.2f2 one was made with 5.4.2f2; I thought that was clear. It is a bug and I've filed a bug report with Unity. You won't be able to duplicate the problem unless you install 5.5.0a6 and if you want to compare it you can open the 5.4.2f2 one with your 5.4 version. – vane Nov 09 '16 at 23:48
  • Just realized that 5.5.0a6 is an Unity 2D Experimental Preview Release 2. Oh well, keep us updated if you hear anything from Unity. – Programmer Nov 10 '16 at 00:54

1 Answers1

1

Just update for someone who may want the answer to this question. In version 2019.1.12f1 of Unity. We have to set 1 more Alpha value after we select the color in the Editor

Click to see how to set Alpha value after select color Then we can use the code normally like:

public Color pink;
public Color purple;
void SetRandomColor() {
        int ran = Random.Range(0, 1);
        Debug.Log("RANDOM: " + ran);
        switch (ran) {
            case 0:
                sr.color = pink;
                break;
            case 1:
                sr.color = purple;
                break;
        }
    }