issue with gameObject.SetActive

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);
}

1920225–123944–FlexOnAnim.cs (428 Bytes)
1920225–123945–FlexOffAnim.cs (402 Bytes)

A GameObject cannot set itself to active.

Yes he can.

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.

Assuming he put the timer in Update(), it can’t as far as I know.

According to Scripting API…

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 ?

Can you give us the entire script ?

At least, the function(s) which handle(s) your timer countdown.

gameObject.GetComponent().enabled = false;

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class TestOff : MonoBehaviour {

    float myTime = 60f;
   
    // Update is called once per frame
    void Update () {
        myTime -= Time.deltaTime;

        if(myTime >= 50) {
            transform.GetComponent<Image>().enabled = true;
        } else if(myTime < 50) {
            transform.GetComponent<Image>().enabled = false;
        }
    }
}

How about this? :slight_smile:

transform.GetComponent<Image>().enabled = myTime >= 50;

Lines saving!

Still compiles down the same but save 1 if statemeant also if you use a other type like Graphic it will work on all ui elements including text

Why don’t use the Timer class and in an OnTimerElapsed event set your GameObject to Active?

Thanks for the feedback…Sbizz & Jeun - that code doesn’t work (see screenshot) is it javascript or what causes that error ?

To use the Image component I believe you have to use the UnityEngine.UI namespace.

You need to add the namespace

using UnityEngine.UI;

That worked - thanks for your help