Problem with coroutine and IEnumerator.

I’ve a problema with it and I don’t know where is it but the level is not loading after enemy is killed. The debug Debug.Log (“BeforWaiting10Seconds”); it shows in console but Debug.Log (“AfterWaiting10Seconds”); it not shows on console.

using UnityEngine;
using System.Collections;

/// Comportamiento de la salud de los objetos

public class SaludBossScript : MonoBehaviour {
	
	
	/// Numero de puntos de salud
	
	public int ps = 300;
	
	private GameObject Boss;
	private ScoreScript scoreScript;
	public int ScoreValue;
	
	
	/// Es jugador o enemigo?
	
	public bool esEnemigo = true;
	
	// LLamamos el script score y el tag enemigo1
	
	void Start ()
	{
		GameObject scoreScriptObject = GameObject.FindGameObjectWithTag ("ScoreScript");
		if (scoreScriptObject != null) {
			scoreScript = scoreScriptObject.GetComponent <ScoreScript> ();
		}
		if (scoreScript == null) {
			Debug.Log ("No se puede encontrar ScoreScript");
		}
		
		Boss = GameObject.FindWithTag ("Boss");
		if (Boss == null) {
			Debug.Log ("No se encuentra a Boss");
		}
	}
	
	
	
	void OnTriggerEnter2D(Collider2D collider)
	{
		//Es un disparo?
		Disparar disparo = collider.gameObject.GetComponent<Disparar>();
		if (disparo != null)
		{
			// Revisamos si es enemigo o compañero
			if (disparo.esDisparoEnemigo != esEnemigo)
			{
				ps -= disparo.daño;
				
				// Destruimos el disparo
				// No colocar solo Destroy()
				//sino eliminara el Script
				Destroy(disparo.gameObject);
				
				if (ps <= 0)
				{
					// Muerto + EfectoEspecial de particulas, += score
					
					scoreScript.AddScore(ScoreValue);
					EfectosEspecialesScript.Instancia.ExplosionBoss1(transform.position);
					EfectosDeSonido.Instancia.ReproducirSonidoBossExplosion();
					Destroy(gameObject);
					StartCoroutine (waitForLevelChange());
				}
				
			}
		}
	}
	void OnCollisionEnter2D(Collision2D coll)
	{
		if (coll.gameObject.tag == "Player")
			ps = 0; 
		EfectosEspecialesScript.Instancia.ExplosionAsteroide(transform.position);
		Destroy(Boss); 
	}
	IEnumerator waitForLevelChange()
	{
				Debug.Log ("BeforWaiting10Seconds");
				yield return new WaitForSeconds (10);
				Application.LoadLevel ("Level2Scene");
				Debug.Log ("AfterWaiting10Seconds");
		}
}

As OnTriggerEnter2D () you destroy object and after you cause Coroutine. It won’t work as it to be destroyed together with object. For example, for such a case, make one more class and attach it to Main Camera. And further use GetComponent. Code see below:

 using UnityEngine;
 using System.Collections;

 public class myLoadLevel : MonoBehaviour {

  public void myLoading() {
   StartCoroutine (waitForLevelChange());
  }

  IEnumerator waitForLevelChange() {
   Debug.Log ("BeforWaiting10Seconds");
   yield return new WaitForSeconds (10);
   Debug.Log ("AfterWaiting10Seconds");
   Application.LoadLevel ("Level2Scene");
  }
 }

Attach this class to Main Camera, or other object which isn’t destroyed. And use GetComponent for Coroutine from your class SaludBossScript:

 void OnTriggerEnter2D(Collider2D collider) {
  ...
  if (ps <= 0) {
   // Muerto + EfectoEspecial de particulas, += score
   scoreScript.AddScore(ScoreValue);
   EfectosEspecialesScript.Instancia.ExplosionBoss1(transform.position);
   EfectosDeSonido.Instancia.ReproducirSonidoBossExplosion();
   Destroy(gameObject);
   Camera.main.GetComponent<myLoadLevel>().myLoading(); // if attach to camera, than get components from it camera.
  }
  ...
 }

I hope that it will help you.