Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

issue with gameObject.SetActive

Discussion in 'Scripting' started by j_campbell, Jan 14, 2015.

  1. j_campbell

    j_campbell

    Joined:
    Dec 8, 2014
    Posts:
    33
    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);
    }
     

    Attached Files:

  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    A GameObject cannot set itself to active.
     
  3. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    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.
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Assuming he put the timer in Update(), it can't as far as I know.
     
  5. jeun

    jeun

    Joined:
    Jan 14, 2015
    Posts:
    4
    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;
    }
     
    Last edited: Jan 14, 2015
  6. j_campbell

    j_campbell

    Joined:
    Dec 8, 2014
    Posts:
    33
    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 ?
     
  7. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Can you give us the entire script ?

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

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    gameObject.GetComponent<Graphic>().enabled = false;
     
  9. jeun

    jeun

    Joined:
    Jan 14, 2015
    Posts:
    4
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class TestOff : MonoBehaviour {
    6.  
    7.     float myTime = 60f;
    8.    
    9.     // Update is called once per frame
    10.     void Update () {
    11.         myTime -= Time.deltaTime;
    12.  
    13.         if(myTime >= 50) {
    14.             transform.GetComponent<Image>().enabled = true;
    15.         } else if(myTime < 50) {
    16.             transform.GetComponent<Image>().enabled = false;
    17.         }
    18.     }
    19. }
    How about this? :)
     
  10. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Code (CSharp):
    1. transform.GetComponent<Image>().enabled = myTime >= 50;
    Lines saving!
     
  11. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    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
     
  12. Deleted User

    Deleted User

    Guest

    Why don't use the Timer class and in an OnTimerElapsed event set your GameObject to Active?
     
  13. j_campbell

    j_campbell

    Joined:
    Dec 8, 2014
    Posts:
    33
    Thanks for the feedback...Sbizz & Jeun - that code doesn't work (see screenshot) is it javascript or what causes that error ?
     

    Attached Files:

  14. Deleted User

    Deleted User

    Guest

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

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    You need to add the namespace

    Code (CSharp):
    1. using UnityEngine.UI;
     
  16. j_campbell

    j_campbell

    Joined:
    Dec 8, 2014
    Posts:
    33
    That worked - thanks for your help