- Home /
How Do I delay the time in which my object turns active?
How Do I delay the time in which my object turns active? I've already looked into
void return new WaitForSeconds()
Maybe I'm not appling it correctly within my code? The following is the code which triggers an object to switch to active after player has entered collider.
enter code hereusing UnityEngine;
public class ActivateTrigger : MonoBehaviour {
public enum Mode {
Trigger = 0, // Just broadcast the action on to the target
Replace = 1, // replace target with source
Activate = 2, // Activate the target GameObject
Enable = 3, // Enable a component
Animate = 4, // Start animation on target
Deactivate= 5 // Decativate target GameObject
}
/// The action to accomplish
public Mode action = Mode.Activate;
/// The game object to affect. If none, the trigger work on this game object
public Object target;
public GameObject source;
public int triggerCount = 1;///
public bool repeatTrigger = false;
void DoActivateTrigger () {
triggerCount--;
if (triggerCount == 0 || repeatTrigger) {
Object currentTarget = target != null ? target : gameObject;
Behaviour targetBehaviour = currentTarget as Behaviour;
GameObject targetGameObject = currentTarget as GameObject;
if (targetBehaviour != null)
targetGameObject = targetBehaviour.gameObject;
switch (action) {
case Mode.Trigger:
targetGameObject.BroadcastMessage ("DoActivateTrigger");
break;
case Mode.Replace:
if (source != null) {
Object.Instantiate (source, targetGameObject.transform.position, targetGameObject.transform.rotation);
DestroyObject (targetGameObject);
}
break;
case Mode.Activate:
targetGameObject.active = true;
break;
case Mode.Enable:
if (targetBehaviour != null)
targetBehaviour.enabled = true;
break;
case Mode.Animate:
targetGameObject.animation.Play ();
break;
case Mode.Deactivate:
targetGameObject.active = false;
break;
}
}
}
void OnTriggerEnter (Collider other)
{
DoActivateTrigger ();
}
}
Not tested, but try this:
In the switch case:
case $$anonymous$$ode.Activate:
StartCoroutine("Call$$anonymous$$e$$anonymous$$aybe"); // wait 5 seconds, and continue operation as usual.
targetGameObject.active = true;
break;
//Outside the DoActivateTrigger () function.
int waitTime = 5;
IEnumerator Call$$anonymous$$e$$anonymous$$aybe() {
yield return new WaitForSeconds(time);
}
@Starwalker - this will not work as is. Run a quick test, and you will see that the switch statement will execute immediately.
StartCoroutine(NameYourIEnumerator()); //call the IEnumerator in trigger.
//setup the coroutine youll be calling outside of update.
IEnumerator NameYourIEnumerator() { return yield new WaitForSeconds(5) //do stuff after 5 seconds. }
$$anonymous$$ight have new / yield backwards... on my phone so I didnt use code box.
Thank you for your responses! No Dice! I get error: 'IEnumerator' couold not be found. $$anonymous$$issing a using directive or an assemble reference?
Can you test the code from here in an empty project?
The reason being that if IEnumerator is not identified by $$anonymous$$ono Compiler then you have bigger problems..
Answer by RyanZimmerman87 · Mar 25, 2014 at 12:16 AM
Hmm I've always been able to use StartCoroutine and IEnumerator just fine in C# with the default MonoDevelop set up, didn't need any additional libraries.
Are you using Visual Studio or something?
If you are using MonoDevelop my guess is you just have some simple syntax error which is causing it to not compile.
You should post a copy of your script including your StartCoroutine and IEnumerator work. I'm guessing there's just something about your code that isn't set up quite right.
It should be as easy as this example to get this to work and in my experience this is by far the best way to work with specific delay timings for my project.
//start coroutine this should only be
//called once per delay cycle
StartCoroutine(delayEnumerator(delayTime));
//IEnumerator performs the delay
IEnumerator delayEnumerator(float newDelayTime)
{
//waits for the seconds sent from the
//delayTime in StartCoroutine
yield return new WaitForSeconds(newDelayTime);
//do the logic which you want to occur
//after the delay
}
You are setting this whole system up a lot differently than anything I've done. I don't ever use switch statements or even public enum, so I can't really comment too much on the other logic but getting a delay with a coroutine should be simple if none of your other logic is interfering.
I'm assuming you want the delay to continue triggering for your DoActivateTrigger() since you only call it once with OnTriggerEnter()?
Your answer
Follow this Question
Related Questions
destroy object after a delay? 6 Answers
Waiting for fractions of a Second 1 Answer
Mouse event build up? 0 Answers
I am trying to make my enemy AI have an attack delay 1 Answer
How do i add a delay before Application.LoadLevel(0) 1 Answer