Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to run a function every time the object the script is attached to gets set to active.

Discussion in 'Scripting' started by EliteWalrus, Aug 23, 2014.

  1. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    I have it set up like this:
    1. Enemy get destroyed
    2. GUI pops up
    3. The script that is attached to the GUI tells it to wait for 2 seconds and then disable itself.

    The problem is, I don't know how start a function as soon as the object is SetActive. I've tried the Start function of course (as seen below) but when the object gets enabled for a second time the Start function does not run again.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var delay : float = 2;
    4.  
    5. function Start ()
    6. {
    7.     yield WaitForSeconds(delay);
    8.     gameObject.SetActive(false);  
    9. }
    10.  
     
    cardan3312 likes this.
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
  3. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44