Question by
curleyjohnson · Oct 12, 2015 at 06:48 PM ·
delayplaymusicrespawn
I am making a 2D platform game. I use the following script to respawn a player when he dies. I want to delay the level being reloaded by about 2 seconds, so the death sound can play. It is respawning too fast. I'm a newbie. any help will be appreciated.
using UnityEngine;
namespace UnitySampleAssets._2D
{
public class Restarter : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
audio.Play ();
coinController.screwCount = 0;
Application.LoadLevel(Application.loadedLevelName);
}
}
}
Comment
Best Answer
Answer by dkjunior · Oct 12, 2015 at 07:20 PM
You can use a coroutine to accomplish this:
public class Restarter : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
audio.Play ();
coinController.screwCount = 0;
StartCoroutine (DelayedLoad ());
}
private IEnumerator DelayedLoad()
{
yield return new WaitForSeconds (2.0f);
Application.LoadLevel(Application.loadedLevelName);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612041038im_/https://answers.unity.com/themes/thub/images/avi.jpg)