Question by 
               Padu112 · Nov 25, 2015 at 12:07 AM · 
                c#2dscripting problem2d game2d-platformer  
              
 
              Cannot delay player respawn.
Hello, I am using this script for my game in order to respawn when I die but when I try to delay the respawn with yield nothing happens. Can anyone help me ?
 using UnityEngine;
  using System.Collections;
   public class HealthScript : MonoBehaviour {
     public int hp = 1;
     
     
     public bool isEnemy = true;
     
     
     public void Damage(int damageCount)
     {
 
         hp -= damageCount;
         
         if (hp <= 0)
         {
         // 'Splosion!
         SpecialEffectsHelper.Instance.Explosion(transform.position);
         SoundEffectsHelper.Instance.MakeExplosionSound();
     
             // Dead!
             Destroy(gameObject);
         StartCoroutine(RestartLevel());
         }
     }
 IEnumerator RestartLevel(){
     yield return new WaitForSeconds(2);
     Application.LoadLevel(Application.loadedLevel);
 } 
     
     void OnTriggerEnter2D(Collider2D otherCollider)
     {
     // Is this a shot?
     BulletScript1 shot = otherCollider.gameObject.GetComponent<BulletScript1> ();
     if (shot != null) {
         // Avoid friendly fire
         if (shot.isEnemyShot != isEnemy) {
             Damage (shot.damage);
                 
             // Destroy the shot
             Destroy (shot.gameObject); 
         }
     }
 }
     }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by gjf · Nov 25, 2015 at 12:06 AM
you're destroying the game object which this scripts runs on, so it won't be able to do anything. if you're reloading the level, then i believe that all game objects will be destroyed automagically, so don't destroy it... or, to be a good citizen, destroy it right before the load level.
Your answer
 
 
             Follow this Question
Related Questions
Detect whether a Rigidbody collided with a Trigger 2 Answers
Teleport 2D Character on TriggerEnter 2 Answers
CS1216 error 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                