- Home /
renderer.enabled doesn't work but coloring does
I have a prefab with it's Sprite renderer disabled (nothing else, no children). I want to enable in the Awake function if it meets a certain condition so I have this code:
if(something){
SpriteRenderer sprite= GetComponent<SpriteRenderer>();
sprite.enabled=true;
}
So the sprite is initially disabled and thereafter is enabled.... but it does not show the sprite! When I put that code in the Update function is always disabled, even when it is changed to enabled by code every frame.
I wanted to check if the sprite was correct so I tried this: sprite.color=Color.red; and this works and the sprite turns to red
So I tried this also: sprite.color=Color.clear; and the sprite dissapears. Do I have to use the coloring to enable/disable the sprite or does anybody knows why disable/enable is not working? Thanks in advance
Is it a Unity bug? It's not possible that coloring affects the sprite but enable/disable not.
Can u post the sprite that u are using, and maybe some screnshoot of the unity showing the problem?
Answer by desiloh · May 12, 2014 at 09:21 PM
I found the problem, it is in the Animator Controller, the first State has nothing and there is a transition to an animation activated by a trigger. If I delete that animation (that is not being played) everything works.
Oh my, I was thinking of asking whether you had an animation setting that state, but it seemed like a silly proposition.
Sorry, I'm new with Unity and I thought that if I changed the state by code it would prevail over the animator, because I wasn't disabling it in that default state, it has nothing there.
Ok, good. I was going to ask if you had something elsewhere that might keep changing it to "invisible."
Thank you! You gave me the solution. =) Same problem here...
Answer by Slulego · May 12, 2014 at 07:18 PM
In your prefab use the check button to uncheck "Sprite Renderer" and add your script to the prefab. In your script use something like:
function Start() {
if(true) {
renderer.enabled = true;
}
}
First give that a try, then add your if statement details.
In this moment I have a prefab with an unchecked "Sprite Renderer" in the Inspector, and a script with this:
private SpriteRenderer sprite;
void Awake(){
sprite= GetComponent<SpriteRenderer>();
}
void Update(){
gameObject.renderer.enabled=true;
renderer.enabled=true;
sprite.enabled=true;
}
No 'if' statement and of course those 3 lines are not necessary, it needs just one of them, but the three of them affects the same sprite. I have two breakpoints in the first and the second line and and when it reaches the second line it is already in true. The next time it is at the first breakpoint it is in false again. I hope to be clear. Thanks
I made another script and left just gameObject.renderer.enabled in the Update function, and attached it to another sprite. The result is similar but different: it shows the sprite once and then disappears again. I don't understand this behavior.
Your answer
Follow this Question
Related Questions
[2D] [C#] Sprite disappears after changing position 1 Answer
hide animated Sprite by code (SpriteRenderer) 2 Answers
How to access SpriteRenderer Color from a shader? 1 Answer
How to choose a size/resolution for the sprites used as game assets in Unity2D ? 2 Answers
Set Gameobject a certain color based off of other game objects 1 Answer