- Home /
Problem is not reproducible or outdated
Why is SpriteRenderer color working in Inspector but not Game View at runtime?
I asked this over on stackoverflow but I figured I'd ask it here as well.
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.
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.5.2f2 which DOES NOT contain the issue.
In SetTurn(bool isPlayer1)
function, first you're changing the sprite colors to Gray and then acc to the condition, you're resetting one of the color to White again. Do both of the sprite renderers have white color or only the other one?. And also, you have 2 SpriteRenderer and maybe looking at only one of them?
None of them change at all. Take a look in my post and you'll see that I went so far as to remove that function altogether and replace it with a simple set of the color on 1 sprite. In my most recent update, I've found that the problem only exists in the 5.5.0a6 2d preview and not the 5.4.2f2 stable release.
Answer by tanoshimi · Nov 09, 2016 at 06:53 PM
The fact that you're only experiencing this in an experimental pre-alpha build of Unity means you should probably report it to the testing group on the appropriate forum rather than here: https://forum.unity3d.com/forums/2d-experimental-preview.104/
I did already and when I posted it here initially I didn't realize it was an issue only affecting the preview build and since have tested both versions.
Follow this Question
Related Questions
Erase certain color from sprite 0 Answers
SpriteRender.color not changing the color 1 Answer
Changing color on pixel art 1 Answer
Vector Graphics Package Change sprite color 4 Answers
Array of colors not working, all sprites turn white. 1 Answer