Hi - I have a timer countdown from 100>0.
I have a canvas with two images. Each image has a png sequence added to its source image via 2 separate animations. Each of these png sequences play back fine.
When the timer reading is above 50 I want “.png sequence A” to be visible and sequence B to be invisible.
When timer reading is below 50 I want “.png sequence B” to be visible and sequence A to be invisible.
I have two scripts - one applied to each GUI image (see attached).
The code is only impacting on one of the image sequences…sequence A disappears once the energy value decreases below 50 but sequence B stays invisible. Any help would be great.
if (Timer.energy >= 50) {
gameObject.SetActive(true);
}
if (Timer.energy <= 49) {
gameObject.SetActive(false);
}
Did you put some Debug.Log just to be sure you go inside the two if statements ?
If your timer countdown is inside the Update function of the gameObject, it will not work ; if you disable your gameObject, Update doesn’t work anymore. You should have a manager, which handles the timer and which calls functions associated to your gameObject. Or, the manager itself handles the gameObject by activating or disabling it.
Making a GameObject inactive will disable every component, turning off any attached renderers, colliders, rigidbodies, scripts, etc… Any scripts that you have attached to the GameObject will no longer have Update() called
solution :
if (Timer.energy >= 50) {
gameObject.renderer.enabled = true;
}
if (Timer.energy <= 49) {
gameObject.renderer.enabled = false;
}
Thanks but the scripts have been applied to UI image components (unity 4.6) - these components don’t have a renderer. I get the following erro:
‘There is no renderer attached to the game object but a script is trying to access it’
I’ve added a sprite renderer component to the UI Images but this doesn’t work.
Any ideas ?