Delay OntriggerEnter

Hey guys, i cant seem to find a solution to make delay on my OnTriggerEnter, have tried yield return WaitForSecends(2) but yield return does not seem to work with OnTriggerEnter, any one who can help me?

My code look like this right now:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Teleporting : MonoBehaviour
{

}

public void OnTriggerEnter(Collider other)
{

SceneManager.LoadScene (“Levels”);

}

I get the feeling there is some method-based way to do that in Unity that would be more optimal than using a timer, but you can always use timers. Here are a couple:

float timer = 0;
timer = timer + Time.deltaTime;
if (timer > 10)
{
doSomething()
}

Then you can have another function do something. Of course you have to determine when and where to reset your timer.

Here’s another:

while (timer < 10)
{
timer += Time.deltaTime;
}

return;

You could also use a for-loop.

for(i = 0, i < 10, i += Time.deltaTime)
{
doSomething();
}

If you don’t know why Time.deltaTime results in “1 second” - spend today wrapping your brain around that; it’s a simple but important concept you need to know to program most anything in unity. Imagine arbitrarily long frames-

Time.deltaTime is the amount of time it took to complete the last frame. If each frame is a half-second long - it will take two frames to reach 1 - in one second. This logic continues no matter how short the frames are.

It doesnt make a delay on my OnTriggerEvent, but thanks for saying what Time.deltaTime is

I don’t know if you can delay the call to OnTrigger, but I know you can delay anything it does when it’s called.

I’m pretty sure the OnTriggerXXX methods can be coroutines. But if not consider using Invoke or stating another coroutine.

I’m not sure if that solves his problem because wouldn’t the first iteration of the coroutine still happen instantly? I have only been using coroutines for a day - I have written a total of two ever.

IEnumerator OnTriggerEnter (collider other){
    yeild return new WaitForSeconds(2);
     Debug.Log("something";
}

// Alternatively

void OnTriggerEnter (Collider other) {
    StartCoroutine(Delay());
}

IEnumerator Delay(){
    yield return new WaitForSeconds(2);
    Debug.Log("Something");
}
3 Likes

Oh, duh. Just yield right away :wink: thanks for educating.

1 Like

Thanks alot BoredMormon! it helped! and thanks clearoutlines for learning me what Time.deltaTime was :slight_smile: Habby programming guys, and have a great next week :slight_smile:

Sorry for bad english

Oh wait, i actually have one more question, im trying to make a timer, but cant seem to figure out how to show it on GUI

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;

public class Timer : MonoBehaviour {
public Time Timerup;
public Text TimeText;

private float Seconds = 0.0f;
private float Minutes = 0.0f;
private float Hours = 0.0f;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
Seconds += Time.deltaTime;
if (Seconds > 60) {
Minutes += 1;
Seconds = 0;
//SetTimeText ();

}

if (Minutes > 60) {
Hours += 1;
Minutes = 0;
}

This is my script. if you have any idea how to show it on my GUI it could me really nice!