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);
}
}
}
}