using UnityEngine;
using System.Collections;
public class DestroyBoundary : MonoBehaviour {
public GUIText warn;
public float showtime = 3.0f;
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
StartCoroutine(Warn());
Application.LoadLevel(Application.loadedLevel);
}
Destroy(other.gameObject);
}
IEnumerator Warn()
{
Time.timeScale = 0;
warn.text = "Please DO NOT Out our game area";
yield return new WaitForSeconds(showtime);
}
}
I think you need to learn the basics of coding. Each line of code does not wait for the previous line of code to finish running.
Your error:
// This will run
StartCoroutine(Warn());
// This line will start IMMEDIATELY after the previous one without waiting for it to finish.
Application.LoadLevel(Application.loadedLevel);
If you want it to work, you have to put the loading level line inside of the IEnumerator.
IEnumerator Warn()
{
Time.timeScale = 0;
warn.text = "Please DO NOT Out our game area";
yield return new WaitForSeconds(showtime);
Application.LoadLevel(Application.loadedLevel); // Add this line here.
}
Maybe you should try this…
//////////////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using System.Collections;
public class DestroyBoundary : MonoBehaviour
{
public GUIText warn;
public float showtime = 3.0f;
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
StartCoroutine(Warn());
}
Destroy(other.gameObject);
}
IEnumerator Warn()
{
Time.timeScale = 0;
warn.text = "Please DO NOT Out our game area";
yield return new WaitForSeconds(showtime);
Application.LoadLevel(Application.loadedLevel);
}
}