Activate GameObject after 4 minutes

Hi, I’ve searched around for this particular issue and the closest answer I could find was this:

using UnityEngine;
   
    public class DelayedActivate : MonoBehaviour {

       
        public void ActivateAfter(float time) {
            StartCoroutine(ActivateRoutine(time));
    }

       
        private IEnumerator ActivateRoutine(float time) {
            yield return new WaitForSeconds(time);
            gameObject.active = false;
        }
    }

The guy who posted this says: “Attach the following script to the gameobject you wish to activate after a time, then call ActivateAfter(time) with the time in seconds after which you would like to activate the game object.”

But I’m not sure how to call “ActivateAfter(time)” and how it should be written in code, can anybody help please? All i need to do is set a game object to activate after 4 minutes of gameplay regardless of what else happens.Thnx

You could for example do something like this:

GameObject.Find("DelayedGameObject").GetComponent<DelayedActivate>().ActivateAfter( 60 * 4 );

The delayed object in my scene is called “log2”, tried this: GameObject.Find("log2").GetComponent<DelayedActivate>().ActivateAfter( 60 * 4 ); But I could really use some more info Im new to programming and I don’t know whats missing, the error message says “DelayedActivate” could not be found…

You could use Invoke to achieve your task:

void Start(){ // Use Start or Awake depending on your goal
    Invoke("wakeup", 240f);  // Time in seconds
}

void wakeup(){
    gameObject.active = true;
}
1 Like

Or do it entirely in Start

IEnumerator Start()
{
    yield return new WaitForSeconds(240);
    gameObject.SetActive(true);
}
1 Like

Note that @KelsoMRK and my scripts need to be on an active object in order to run.

Good point. While Start will run regardless of active state, disabling an object kills any coroutines on it.

Yeah, it won’t be possible for an object to activate itself after 4 minutes. Functions can run on inactive objects; coroutines cannot.

I’m unsure if Invoke() would be called on inactive objects. TBH, in this case, I would probably have a separate manager object that runs the coroutine and activates the other one.

This is a bit more code… but this is the way I may be doing it :wink:

First you make an “empty gameobject”.
Then you put the gameobject(s) that you want to appear after some time in the “empty gameobject”. it/they have to be enabled.
And finally attach this script to the “empty gameobject”…

using UnityEngine;
using System.Collections;

public class DelayedSetActive : MonoBehaviour {

    void Start() {
         StartCoroutine(ActivateRoutine());
    }
  
    private IEnumerator ActivateRoutine() {

        // make a list of all children
        Transform[] ChildrenTransforms = this.gameObject.GetComponentsInChildren<Transform>();
        // the gameObjects have to be enabled, otherwise they will not be found ...
        foreach( Transform t in ChildrenTransforms)
            if( 0 == t.childCount)                // skip the parent, probably the first one. Sorry: no children in the child-objects !
                t.gameObject.SetActive (false); // disable the children

        yield return new WaitForSeconds(4.0f * 60.0f);    // and now we wait !

        foreach( Transform t in ChildrenTransforms)
            t.gameObject.SetActive (true);      // restore all the disabled objects in the list (even the parent)
          
    }
}

I hope this works for you :sunglasses:
(not to many spelling mistakes… I hope)

Whoa all these replies requiring other objects, etc. Just do this:

using UnityEngine;
  
    public class DelayedActivate : MonoBehaviour {

       void Start(){
ActivateAfter(60*4); 
}
        public void ActivateAfter(float time) {
            StartCoroutine(ActivateRoutine(time));
    }

      
        private IEnumerator ActivateRoutine(float time) {
            yield return new WaitForSeconds(time);
            gameObject.active = false;
        }
    }
1 Like

You tried to be clever and provide a solution only to provide a solution that is the exact opposite of what OP wants :slight_smile:

OP wants to turn an object on after a set amount of time. You can’t do it with coroutines because they don’t run on inactive objects - otherwise what you wrote is a more verbose inversion of what I wrote.

1 Like

Well look who didn’t read over the code properly (me) :p, oops!

1 Like

Well… KelsoMRK…that’s the trick.
The “empty gameobject” does not get disabled, that are running the script. [if( 0 == t.childCount)]
I need the children to be enabled on Start() otherwise GetComponentsInChildren wouldn’t find them.
Then all children get disabled, but are remembered in the ChildrenTransforms.
Then wait a while…
and all children get enabled again.

Ways_to_code_something > π * programmers.numberOf()

FWIW there’s an overload of GetComponentsInChildren that will return inactive components :slight_smile: You just have to take care to exclude the top-most parent as it will come back as well.

1 Like

OK
You are right! That’s a better way, KelsoMRK, but I didn’t know about it :(. Thanks!
Enabling the parent that’s already enabled doesn’t seem to matter.

Start with the children disabled !

using UnityEngine;
using System.Collections;

public class DelayedSetActive : MonoBehaviour {

    void Start() {
         StartCoroutine(ActivateRoutine());
    }
  
    private IEnumerator ActivateRoutine() {

       yield return new WaitForSeconds(4.0f * 60.0f);    // and now we wait !
       
        // make a list of all children
        Transform[] ChildrenTransforms = this.gameObject.GetComponentsInChildren<Transform>(true);
        foreach( Transform t in ChildrenTransforms)
            t.gameObject.SetActive (true);      // enable all the objects in the list (even the parent)
      
    }
}
1 Like

I’ve done this via Linq before to get every child Transform while excluding the parent this script is attached to.

Transform[] children = GetComponentsInChildren<Transform>(true).Where(t => t != transform).ToArray();

Hi guys, Sorry for the late response. I really appreciate all the replies and KelsoMRK and TEBZ_22’s version worked perfectly. I attached this script to the parent and disabled the child and voila!

using UnityEngine;
using System.Collections;
public class DelayedSetActive : MonoBehaviour {
    void Start() {
         StartCoroutine(ActivateRoutine());
    }
    private IEnumerator ActivateRoutine() {
       yield return new WaitForSeconds(4.0f * 60.0f);    // and now we wait !
      
        // make a list of all children
        Transform[] ChildrenTransforms = this.gameObject.GetComponentsInChildren<Transform>(true);
        foreach( Transform t in ChildrenTransforms)
            t.gameObject.SetActive (true);      // enable all the objects in the list (even the parent)
    
    }
}

You guys rule! :wink: