Respawn issues
Hello there. I've got some troubles with my respawn script. I've read a lot about respawning scripts but haven't found what would be the best for my video game :< I think that almost everything is ok in this script, except when i use "Debug.Log("message");", I figure out that this part of the script is not executed. Could someone help me ? Thanks !
public float maxHealth = 1000f;
public float curHealth = 0f;
private bool isAlive = true;
private float timer;
private int waitingTime = 2;
public GameObject enemyPrefab;
private GameObject spawnPos;
private StatsMage statsMage;
void Start () {
curHealth = maxHealth;
statsMage = Character.GetComponent<StatsMage>();
enemyPrefab = GameObject.Find("ennemy");
spawnPos = GameObject.Find("spawnPos");
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Weapon")
{
curHealth -= statsMage.DamageDealMage;
}
}
void Update () {
if (curHealth <= 0)
{
isAlive = false;
Destroy(enemyPrefab);
}
if (isAlive == false)
{
timer += Time.deltaTime;
if (timer > waitingTime)
{
Debug.Log("Message");
Instantiate(enemyPrefab,spawnPos.transform);
isAlive = true;
timer = 0;
}
}
}
Answer by Cuttlas-U · Apr 09, 2017 at 04:56 PM
hi; in this situations u better Debug the "if" statement values for example "Timer" or "isAlive" and see if they are doing fine or not cause i don't see any problem in your script;
Answer by Eskahaine · Apr 10, 2017 at 11:54 AM
Thanks for your answer mate !
I debugged the if statement values. isAlive = false, and timer is equal to 0.0152... so I think my timer is kind of broken.
I tried with a StartCoroutine, same problem.
IEnumerator respawn8()
{
Debug.Log("isAlive = " + isAlive);
yield return new WaitForSeconds(2);
isAlive = true;
Debug.Log("isAlive = " + isAlive);
Instantiate(enemyPrefab, spawnPos.transform);
}
void Update () {
if (curHealth <= 0)
{
isAlive = false;
Destroy(enemyPrefab);
}
if (isAlive == false)
{
StartCoroutine(respawn8());
Debug.Log("isAlive = " + isAlive);
}
}
The Debug.Log under StarCoroutine is OK, the first Debug.Log in the respawn8 is OK, but I think that he doesn't want to execute "yield return new WaitForSeconds(2);", I don't know why
Your answer
Follow this Question
Related Questions
How is it possible to verify values and is they are correct activate objects ? 0 Answers
How to instantiate prefabs at certain times? 1 Answer
Trying to instantiatea prefab then destroy it and the gameobject it was spawned on 1 Answer
Why aren't my prefab clones destroying on play mode exit? 1 Answer
Spawn and destroy gameObject by tag 0 Answers