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. Dismiss Notice

Script Causing Unity Crash

Discussion in 'Scripting' started by DeathByRhino, Jan 20, 2016.

  1. DeathByRhino

    DeathByRhino

    Joined:
    Jul 30, 2015
    Posts:
    33
    Something with this script is causing the editor to freeze whenever I transition into the scene that uses it, and I have to close it with task manager. Its a manager that is in charge of which enemy is currently attacking the player. Any advice?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class TurnManager : MonoBehaviour
    6. {
    7.     public GameObject enemyAttacking;
    8.     public List<GameObject> enemylist = new List<GameObject>();
    9.     GameObject[] currentenemies;
    10.     public GameObject PlayerVariables;
    11.  
    12.     void Start()
    13.     {
    14.         EnemyCount();
    15.         StartCoroutine(EnemyTurn());
    16.        
    17.     }
    18.  
    19.     void EnemyCount()
    20.     {
    21.         currentenemies = MultiTags.FindGameObjectsWithMultiTag("foe");
    22.         if (currentenemies != null)
    23.         {
    24.             foreach (GameObject character in currentenemies)
    25.             {
    26.                 enemylist.Add(character);
    27.                 enemylist.Sort(delegate (GameObject a, GameObject b)
    28.                 {
    29.                     return (a.GetComponent<EnemyVariables>().speed).CompareTo(b.GetComponent<EnemyVariables>().speed);
    30.                 });
    31.             }
    32.         }
    33.         else if(currentenemies == null)
    34.         {
    35.             EndCombat();
    36.         }
    37.     }
    38.     void Update()
    39.     {
    40.        
    41.     }
    42.     IEnumerator EnemyTurn()
    43.     {
    44.         foreach (GameObject enemy in enemylist)
    45.         {
    46.             enemyAttacking = enemy;
    47.             Debug.Log("EnemyTurn");
    48.             EnemyScript attackscript = enemy.GetComponent<EnemyScript>();
    49.             attackscript.TurnChosen();
    50.             while (attackscript.attacking)
    51.             {
    52.                
    53.                 yield return null;
    54.                
    55.             }
    56.         }
    57.         Debug.Log("EnemyTurn Finish");
    58.         StartCoroutine(EnemyTurn());
    59.     }
    60.  
    61.     public void EndCombat()
    62.     {
    63.         //enemycountFSM.SendEvent("ExitCombat");
    64.         Debug.Log("EnemyManager Exit");
    65.     }
    66. }
     
  2. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    You are probably getting into an infinite loop somewhere. It happens.
    Just comment-out parts of your code until you find the cause.
     
    Kiwasi likes this.
  3. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    Try doing this instead of using null.

    Code (csharp):
    1. yield return new WaitForSeconds(0.1f);
     
  4. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    @The Little Guy, that wont change anything, null means wait till the next frame.
     
  5. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    Okay, then could it be that the same Coroutine is called within the Coroutine...

    If I am not mistaken this will create tons of Routines and bog the system down to a point where it is un-responsive.
     
  6. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    You're probably right.
    EnemyTurn() directly calls StartCoroutine(EnemyTurn()); which will lead to an infinite loop.
    The OP probably didn't know that when you call StartCoroutine it will call the Couroutine right away and return after the first yield.
     
  7. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    Try this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class TurnManager : MonoBehaviour
    5. {
    6.     public GameObject enemyAttacking;
    7.     public List<GameObject> enemylist = new List<GameObject>();
    8.     GameObject[] currentenemies;
    9.     public GameObject PlayerVariables;
    10.  
    11.     IEnumerator Start()
    12.     {
    13.         EnemyCount();
    14.    
    15.         while(true) // Probably want to replace "true" with a boolean test
    16.         {
    17.             foreach (GameObject enemy in enemylist)
    18.             {
    19.                 enemyAttacking = enemy;
    20.                 Debug.Log("EnemyTurn");
    21.                 EnemyScript attackscript = enemy.GetComponent<EnemyScript>();
    22.                 attackscript.TurnChosen();
    23.                 while (attackscript.attacking)
    24.                 {
    25.                     yield return null;
    26.                 }
    27.             }
    28.             Debug.Log("EnemyTurn Finish");
    29.             yield return null; // Remove if un-needed here
    30.         }
    31.     }
    32.     void EnemyCount()
    33.     {
    34.         currentenemies = MultiTags.FindGameObjectsWithMultiTag("foe");
    35.         if (currentenemies != null)
    36.         {
    37.             foreach (GameObject character in currentenemies)
    38.             {
    39.                 enemylist.Add(character);
    40.                 enemylist.Sort(delegate (GameObject a, GameObject b)
    41.                 {
    42.                     return (a.GetComponent<EnemyVariables>().speed).CompareTo(b.GetComponent<EnemyVariables>().speed);
    43.                 });
    44.             }
    45.         }
    46.         else if(currentenemies == null)
    47.         {
    48.             EndCombat();
    49.         }
    50.     }
    51.  
    52.     public void EndCombat()
    53.     {
    54.         //enemycountFSM.SendEvent("ExitCombat");
    55.         Debug.Log("EnemyManager Exit");
    56.     }
    57. }
     
    DeathByRhino likes this.