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
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…
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
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
(not to many spelling mistakes… I hope)
You tried to be clever and provide a solution only to provide a solution that is the exact opposite of what OP wants
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.
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.
FWIW there’s an overload of GetComponentsInChildren that will return inactive components You just have to take care to exclude the top-most parent as it will come back as well.
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)
}
}
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)
}
}