Reputation: 2215
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.
Upvotes: 4
Views: 4742
Reputation: 13
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;
}
}
Upvotes: 1