I’ve been reading over the C# Yield answers for hours - I cannot seem to make anything work with what I want to do. Half my game is created and I can’t believe this is where I got stuck! Haha!
So, What I want to have happen is a delay inbetween some functions…
`public void executeAttack(int pa){
/*
* Enemy select attack based on players selected attack and weakest defence.
* Attempt to execute an attack BASED ON THE SPEED OF THE PLAYER AND OPPONENT
* To do that, if (PlayerSpeed > Enemy){player.Attack(); enemy.Attack()} else { enemy.Attack(); player.Attack() }
*/
if ((player.Vitals[1] - skillist.skills[pa,5,0,0]) >= 0){ // Check if you have enough AP to attack
enemy.SelectedAttack = enemySelectAttackAI(pa); // Enemy select Attack AI (Currently just random based on what skills the enemy has)
player.SelectedAttack = pa; // Set the player selectedAttack
if(player.Attributes[6] >= enemy.Attributes[6]){ // Attribute[6] = speed
player.Attack(enemy);
//I want to create delays in between these functions!
player.checkStatusEnd();
checkHealth(enemy); // Should make this into a function
checkReason(player);
enemy.Attack(player);
enemy.checkStatusEnd();
checkHealth(player);
checkReason(enemy);
} else if (player.Attributes[6] > enemy.Attributes[6]) { // Need a better way to display messages...
enemy.Attack(player);
yield return new WaitForSeconds( 2.5f );
enemy.checkStatusEnd();
checkHealth(player);
checkReason(enemy);
player.Attack(enemy);
player.checkStatusEnd();
checkHealth(enemy);
checkReason(player);
}
} else {
showMessage("You do not have enough AP to use that attack");
}
}`
Now I tried so many different solutions…
Obviously, I tried make executeAttack return IEnumerator… no joy.
I’ve tried creating while loops that loop through till time > x (x being the delay time)
Most my results end in either
- Nothing at all happening
- Infinite Iterations (Unity Crashes)
- If I call a seperate delay function using StartCoroutine(delay(10)); etc, it just runs along side the code I’m trying to pause.
Most of these are actually expected results… I’m just baffled as to the logic behind making this work.