void OnCollisionEnter(Collision collision){
foreach(var r in GetComponentsInChildren<Renderer>()) //Don't destroy the object yet but make it invisible
r.enabled = false;
StartCoroutine(WaitForSecond());
}
whydoidoit is right that you have to explicitly start the coroutine, otherwise it doesn’t run at all. However there’s another problem. When you start a coroutine, it runs on the MonoBehaviour that starts the coroutine. StartCoroutine is a member function of MonoBehaviour. You need another independent MonoBehaviour that isn’t going to be destroyed next frame.
Usually you would use some kind of game manager script that can handle such cases. For your case you can simple create a seperate script that loads the level:
// LoadLevelDelayed.cs
using UnityEngine;
public class LoadLevelDelayed : MonoBehaviour
{
public float Delay = 2.0f;
public string Level = "";
IEnumerator Start()
{
yield return new WaitForSeconds(Delay);
Application.LoadLevel(Level);
}
public static void Load(string aLevel, float aDelay)
{
LoadLevelDelayed loadLevelScript = (new GameObject()).AddComponent<LoadLevelDelayed >();
loadLevelScript.Level = aLevel;
loadLevelScript.Delay = aDelay;
}
}
When you execute the static Load function it creates a new gameobject and attaches the LoadLevelDelayed script to it which will actually perform the level load