Why does this coroutine crash Unity?
This seems to crash my game every time it is run, can't figure out why. I've taken out the working code, but it's supposed to attack the player every x seconds. [enemStats holds the variables for that]
private bool isAttackingPlayer;
void Update () {
if(!isAttackingPlayer)
attackPlayer ();
}
private void attackPlayer(){
// enemy is in range [Attack!]
StartCoroutine(attackingPlayer(enemStats.AttackSpeed));
// TODO start attack voice & animation
}
IEnumerator attackingPlayer(float waitTime){
isAttackingPlayer = true; // starting to attack
yield return new WaitForSeconds (waitTime); // wait after cd
// wait time ended
if (playerStats.ShieldHP >= 0) // if there is a shield up
playerStats.ShieldHP -= enemStats.Damage;
else
playerStats.PlayerHP -= enemStats.Damage;
isAttackingPlayer = false; // no longer attacking
}
Can we get more details about the "crash?" Is there an error message? Is the entire engine locking up until you close it with ctrl-alt-delete?
Answer by FalxGames · Mar 13, 2017 at 09:45 PM
Managed to locate and fix the problem with the debugger. It turns out what I thought to be an error in my coroutine actually ended up to be a syntax error in the enemStats.AttackSpeed get method. [Accidentally returned the function name instead of the data type, possibly causing an infinite loop]
Thanks for the hasty response, @Commoble! :D
I'll definitely check the debugger first next time!
Your answer
![](https://koobas.hobune.stream/wayback/20220612113115im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
While loop won't work, it freezes Unity 1 Answer
Instantiate is not working in unity 5.6.4f1 0 Answers
Unity crash, now errors and deleted objects reappeared.. 0 Answers
ios app crashes on start 0 Answers
Visual Studio 2015 keeps crashing... 1 Answer