Unity2D I wanna use a delay time on Entering an Trigger.
I wanna use a WaitForSeconds code to have a delay time before Application.LoadLevel and load the next level but i dont get it how.
here is the script:
using UnityEngine; using System.Collections;
public class ForestPortalKey : MonoBehaviour {
void Start ()
{
StartCoroutine(Countdown());
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "Player" && GameVariables.keyCount > 0)
{
GameVariables.keyCount -- ;
}
}
IEnumerator Countdown()
{
yield return new WaitForSeconds (1);
}
}
Your question isn't clear. What does the trigger have to do with this?
Everything before yield return new WaitForSeconds (1);
in the coroutine will happen as soon as it is called, everything after will be called after one second.
Therefore to wait a second before loading the level just put the load command after the WaitForSeconds. If however you want it to wait after entering the trigger as your question says then OnTriggerEnter2D just needs to start the coroutine with whatever event is to be triggered after the WatiForSeconds. Or if you mean that you only want the trigger to be active a second after loading the level then it should toggle a bool which the trigger will check.
Your answer
Follow this Question
Related Questions
Help with a door slam trap 0 Answers
Any way to fix an tag change problem? 1 Answer
Is It Bad to be Tired of my Own Mobile Game? 1 Answer
Save Data Android C# 0 Answers
Code works in editor, but not on Android device(audio mixer) 1 Answer