Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problems with Corutines

Discussion in 'Scripting' started by UndeadMadison, May 19, 2020.

  1. UndeadMadison

    UndeadMadison

    Joined:
    Apr 16, 2020
    Posts:
    15
    Hello! So I am working on an anime turn based game for me and my friends to enjoy but I am stuck on the IEnumerators. I tried stopping them on the opposing ones turn to attack but it isn't working so well cause it still only runs through both Corutines once. I tried to use just normal functions but the health wouldn't go down when I did that and everything got out of sync so I am still attempting with the IEnumerator things. I am still new with those. All the videos I watched were all on press of a key on the keyboard but I don't want that even for the player attack. This is the whole code for the Attack Manager. Sorry if it looks a bit messy, as I learn I am changing things around.

    Thank you!


    Code (CSharp):
    1. public class AttackManager : MonoBehaviour
    2. {
    3.     public AudioSource enemyMakoto;
    4.     public AudioSource makotoNaegiAttack1Sound;
    5.     public AudioSource makotoNaegiAttack1Sound2;
    6.     public AudioSource makotoNaegiAttack2Sound;
    7.     public AudioSource makotoNaegiAttack2Sound2;
    8.  
    9.     public MakotoNaegiAttack makotoNaegi;
    10.     public ShoyoHinetaAttacks shoyoHineta;
    11.     public DanganronpaEnemyAttacks danganronpaEnemyAttacks;
    12.  
    13.     public GameObject[] box1Enemies;
    14.     public GameObject[] box2Enemies;
    15.     public GameObject[] box3Enemies;
    16.  
    17.     public GameObject[] makotoNaegiLVL1Characters;
    18.     public GameObject[] makotoNaegiLVL2Characters;
    19.     public GameObject[] shoyoHinetaLVL1Characters;
    20.     public GameObject[] enemyMakotoNaegi;
    21.  
    22.     public GameObject[] attacks;
    23.  
    24.     public Sprite[] CHaracterMNAttackSprites;
    25.     public Sprite[] CHaracterSHAttackSprites;
    26.     public Sprite[] EnemyMNAttackSprites;
    27.     public Sprite[] waitSprites;
    28.  
    29.     public static bool[] enemy = new bool[3];
    30.     public static bool[] nextEnemy = new bool[3];
    31.  
    32.     public static bool[] characterAttacked = new bool[3];
    33.     public static bool playerTurn;
    34.     public static bool attacked;
    35.  
    36.     public static int[] enemyAttackNumber = new int[1]; //Will add more
    37.  
    38.     public bool[] enemyAttacks;
    39.  
    40.     public static bool heroSelected;
    41.     public static bool enemySelected;
    42.     public static bool enemyTurn;
    43.  
    44.     public bool stopAttacks;
    45.  
    46.     public static int[] audioPlayTimes = new int[8]; //Will Add More
    47.     public static int[] charactersAttackNumbers = new int[3]; //Will Add more
    48.  
    49.     void Start()
    50.     {
    51.         //Players turn to attack
    52.         enemyAttackNumber[0] = 1;
    53.         playerTurn = true;
    54.     }
    55.  
    56.     void Update()
    57.     {
    58.         if(stopAttacks == true)
    59.         {
    60.             stopAttacks = false;
    61.             StopAllCoroutines();
    62.         }
    63.  
    64.         //Switching the turns to enemy
    65.         if (enemyTurn == true && playerTurn == false)
    66.         {
    67.             Debug.Log("Enemy is attacking");
    68.             attacked = true;
    69.             danganronpaEnemyAttacks.MakotoNaegiEnemy();
    70.             IEnumerator enemyAttack = LeaveEnemyAttacks();
    71.             if (enemyAttack != null)
    72.             {
    73.                 StartCoroutine(LeaveEnemyAttacks());
    74.             }
    75.         }
    76.     }
    77.  
    78.     //Player selects the hero they want to fight with, it gives audio feedback so you know who you clicked
    79.     public void SelectHeroAttacker(int characterNumber)
    80.     {
    81.         if (characterNumber == 1 && attacked == false && enemySelected == false && heroSelected == false && playerTurn == true)
    82.         {   //Makoto Naegi
    83.             makotoNaegi.SelectMakotoNaegi();
    84.         }
    85.        
    86.         if (characterNumber == 2 && attacked == false && enemySelected == false && heroSelected == false && playerTurn == true)
    87.         {   //Shoyo Hineta
    88.             shoyoHineta.SelectShoyoHineta();
    89.         }
    90.     }
    91.  
    92.     public void MakotoNaegiAttack(int number)
    93.     {
    94.         IEnumerator playerAttack = LevaePlayerAttack();
    95.         if (number == 1 && enemySelected == true && heroSelected == true && playerTurn == true && playerAttack != null)
    96.         {
    97.             charactersAttackNumbers[0] = 1;
    98.             makotoNaegi.MNAttack(1, CHaracterMNAttackSprites);
    99.             StartCoroutine(playerAttack);
    100.         }
    101.         else if (number == 2 && enemySelected == true && heroSelected == true && playerTurn == true && playerAttack != null)
    102.         {
    103.             charactersAttackNumbers[0] = 2;
    104.             makotoNaegi.MNAttack(2, CHaracterMNAttackSprites);
    105.             StartCoroutine(playerAttack);
    106.         }
    107.         else if (number == 3 && heroSelected == true && playerTurn == true && playerAttack != null)
    108.         {
    109.             charactersAttackNumbers[0] = 3;
    110.             makotoNaegi.HealingCharacters(1);
    111.             StartCoroutine(playerAttack);
    112.         }
    113.     }
    114.  
    115.     public void ShoyoHineta(int number)
    116.     {
    117.         IEnumerator playerAttack = LevaePlayerAttack();
    118.         if (number == 1 && enemySelected == true && heroSelected == true && playerTurn == true && playerAttack != null)
    119.         {
    120.             charactersAttackNumbers[1] = 1;
    121.             shoyoHineta.SHAttack(1, CHaracterSHAttackSprites);
    122.             StartCoroutine(playerAttack);
    123.         }
    124.         else if (number == 2 && enemySelected == true && heroSelected == true && playerTurn == true && playerAttack != null)
    125.         {
    126.             charactersAttackNumbers[1] = 2;
    127.             shoyoHineta.SHAttack(2, CHaracterSHAttackSprites);
    128.             StartCoroutine(playerAttack);
    129.         }
    130.         else if (number == 3 && heroSelected == true && playerTurn == true && playerAttack != null)
    131.         {
    132.             charactersAttackNumbers[1] = 3;
    133.             shoyoHineta.HealingCharacters(1);
    134.             StartCoroutine(playerAttack);
    135.         }
    136.     }
    137.  
    138.     //Allows player to select an enemy that they want to attack
    139.     public void EnemySelect(int enemyNumber)
    140.     {
    141.         if (enemyNumber == 1 && heroSelected == true && attacked == false && enemySelected == false && playerTurn == true)
    142.         {
    143.             //Enemy Makoto
    144.             enemyMakoto.Play();
    145.             enemySelected = true;
    146.             enemy[0] = true;
    147.         }else if (enemyNumber == 2 && heroSelected == true && attacked == false && enemySelected == false && playerTurn == true)
    148.         {
    149.             //Enemy Makoto
    150.             enemyMakoto.Play();
    151.             enemySelected = true;
    152.             enemy[1] = true;
    153.         } else if (enemyNumber == 3 && heroSelected == true && attacked == false && enemySelected == false && playerTurn == true)
    154.         {
    155.             //Enemy Makoto
    156.             enemyMakoto.Play();
    157.             enemySelected = true;
    158.             enemy[2] = true;
    159.         }
    160.     }
    161.  
    162.     IEnumerator LevaePlayerAttack()
    163.     {
    164.         Debug.Log("Leave Player Attack");
    165.         makotoNaegiLVL1Characters[0].GetComponent<Image>().sprite = waitSprites[0];
    166.         makotoNaegiLVL1Characters[1].GetComponent<Image>().sprite = waitSprites[0];
    167.         makotoNaegiLVL1Characters[2].GetComponent<Image>().sprite = waitSprites[0];
    168.         makotoNaegiLVL2Characters[0].GetComponent<Image>().sprite = waitSprites[1];
    169.         makotoNaegiLVL2Characters[1].GetComponent<Image>().sprite = waitSprites[1];
    170.         makotoNaegiLVL2Characters[2].GetComponent<Image>().sprite = waitSprites[1];
    171.         shoyoHinetaLVL1Characters[0].GetComponent<Image>().sprite = waitSprites[2];
    172.         shoyoHinetaLVL1Characters[1].GetComponent<Image>().sprite = waitSprites[2];
    173.         shoyoHinetaLVL1Characters[2].GetComponent<Image>().sprite = waitSprites[2];
    174.      
    175.         yield return new WaitForSeconds(6f);
    176.        
    177.         attacked = false;
    178.         enemyTurn = true;
    179.         playerTurn = false;
    180.  
    181.         heroSelected = false;
    182.         enemySelected = false;
    183.  
    184.         enemy[0] = false;
    185.         enemy[1] = false;
    186.         enemy[2] = false;
    187.         stopAttacks = true;
    188.     }
    189.  
    190.     IEnumerator LeaveEnemyAttacks()
    191.     {
    192.         Debug.Log("Working");
    193.  
    194.         yield return new WaitForSeconds(3f);
    195.  
    196.         enemyMakotoNaegi[0].GetComponent<Image>().sprite = EnemyMNAttackSprites[0];
    197.         enemyMakotoNaegi[1].GetComponent<Image>().sprite = EnemyMNAttackSprites[0];
    198.         enemyMakotoNaegi[2].GetComponent<Image>().sprite = EnemyMNAttackSprites[0];
    199.  
    200.         if (nextEnemy[0] == true && enemy[1] == true && enemy[2] == true)
    201.         {
    202.             nextEnemy[0] = false;
    203.             nextEnemy[1] = false;
    204.             nextEnemy[2] = false;
    205.         }
    206.         enemyTurn = false;
    207.         playerTurn = true;
    208.         attacked = false;
    209.         stopAttacks = true;
    210.     }
    211. }
     
    Last edited: May 19, 2020
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  3. UndeadMadison

    UndeadMadison

    Joined:
    Apr 16, 2020
    Posts:
    15
    Joe-Censored likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So you want your coroutines to repeat? Usually then you include a loop in the coroutine and yield within the loop. As written I'd expect them to immediately run the code at the top of the coroutine, pause at the yield line, and when they resume they execute the rest of the coroutine, and the coroutine exits since there is no more code within the coroutine to rune.

    Code (csharp):
    1. void Start()
    2. {
    3.     StartCoroutine(beSilly);
    4. }
    5.  
    6. IEnumerator beSilly()
    7. {
    8.     while (true)
    9.     {
    10.         Debug.Log("I'm a silly coroutine, short and stout");
    11.         yield return new WaitForSeconds(3f);
    12.     }
    13. }
    You can make it an infinite loop as above, where you either stop the coroutine manually or just let it run forever until the GameObject/Component is destroyed, or you can have some exit condition for the loop. The exit condition for the loop becomes effectively the exit condition for the coroutine.
     
    Last edited: May 19, 2020
  5. UndeadMadison

    UndeadMadison

    Joined:
    Apr 16, 2020
    Posts:
    15
    I added the while loops. I attempted to leave it like that but it didn’t do anything. So I have to call it in the Start function? It’s like I don’t know if that will cause the attacks to not work anymore or not cause it’s constantly going. The attacks are in functions in another code and depending on specific bools it is supposed to activate the next corutine to reset everything for the next attack from the next character.
     
    Last edited: May 20, 2020