- Home /
Why is my Unity Crashing when I call a fucntion within coroutine and vice versa?
So the idea is that each time a function runs, an enemy damages the player, and then the coroutine waits until the player hits space to strike back. This loop would go on until either side is dead. What is the reason unity just freezes when it comes to this function?
void monsterBattle() { int atk = Random.Range(3, 7); takeDmg(ref knight, atk); textComp.text = string.Format("As you step into a cave a monster attacks you!...\nThe monster have dealt ",atk.ToString(),"!"); //StartCoroutine(FightBack()); } IEnumerator FightBack() { bool pressed = false; while (!pressed) {
if (Input.GetKey(KeyCode.Space))
{
Debug.Log("pressed");
pressed = true;
monsterBattle();
break;
}
yield return null; //you might want to only do this check once per frame -> yield return new WaitForEndOfFrame();
}
}
Your answer
Follow this Question
Related Questions
Loop a function 1 Answer
StopCoroutine() is not stopping my coroutines 1 Answer
Simplify this code 3 Answers
How To Make UI Button 2 Appear Three Seconds After UI Button 1 is pressed 1 Answer
Cutscene Segments 1 Answer