I have been trying for a while to get the player to respawn without the player having to restart the game. However, no matter what variation I try, I cannot seem to get 1 player to be in the game. Currently, it creates a loop making tons of players. How could I fix my current script or redesign it to make it do what it is designed to?
The variable play is set to a prefab of the player
Also, bonus points if you can find out how to fix my regen script to regenerate 10 health every 5 seconds.
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
public float MaxHealth=100;
public float CurrentHealth;
public bool Invincible;
public bool Dead;
public bool run = false;
public bool regen = true;
public Transform play;
public Transform fplay;
// Use this for initialization
void Awake () {
//MAKE THE CURRENT HEALTH THE MAX HEALTH AT START
CurrentHealth=MaxHealth;
}
//regen doesnt work yet
IEnumerator Start() {
if(regen=true && CurrentHealth>0){
yield return new WaitForSeconds(5);
run = true;
yield return 0;
run = false;
}
}
// Update is called once per frame
void Update () {
//IF INVINCIBLE, HE CANNOT DIE..
if(Invincible){
CurrentHealth=MaxHealth;
}
else{
if(CurrentHealth<=0){
CurrentHealth=0;
Dead=true;
}
//MAX HEALTH
if(CurrentHealth>=MaxHealth)CurrentHealth=MaxHealth;
if(run){
CurrentHealth=(CurrentHealth+=10);
}
//WHEN DEATH IS UPON HIM
if(Dead){
CurrentHealth=MaxHealth;
var cplay = GameObject.Find ("Player");
var obj = GameObject.Find ("Respawn");
Instantiate(play, obj.transform.position, Quaternion.identity);
Dead=false;
Destroy (gameObject);
}
}
}
}