Search Unity

[Solved] IEnumerator crashing Unity?

Discussion in 'Scripting' started by fieldforensics, Feb 8, 2018.

  1. fieldforensics

    fieldforensics

    Joined:
    Aug 23, 2017
    Posts:
    14
    Not quite sure but through several series of debugging I've formed a conclusion that once I yield Unity/Application freezes and stops responding. No stack trace error.
    I've tried a few things, even came with the idea that perhaps you can only StartCoroutines within the same script, that being why I created the begin method. I believe it's important to note that the project/script used to work fine until I decided after some time to return to it about a month later and noticed that Unity would freeze/crash. Perhaps I changed something, but it doesn't appear to make sense for Unity to crash upon returning null to the IEnumerator.
    Code (CSharp):
    1.     public void begin(GameObject[] Roads, GameObject[] Sidewalks, int Size, float MinimumRoadLengthX, float MaximumRoadLengthX, float MinimumRoadLengthY, float MaximumRoadLengthY)
    2.     {
    3.         Debug.Log("Beginning Road Coroutine!");
    4.         StartCoroutine(generateStreets(Roads, Sidewalks, Size, MinimumRoadLengthX, MaximumRoadLengthX, MinimumRoadLengthY, MaximumRoadLengthY));
    5.     }
    6.  
    7.     private IEnumerator generateStreets(GameObject[] Roads, GameObject[] Sidewalks, int Size, float MinimumRoadLengthX, float MaximumRoadLengthX, float MinimumRoadLengthY, float MaximumRoadLengthY)
    8.     {
    9.         Debug.Log("generating streets...");
    10.         Debug.Log("1");
    11.         yield return null; //crashes here
    12.         Debug.Log("2");
    13.         RoadComplete = true;
    14.         Debug.Log("Road Completed!");
    15.     }
    Any ideas?
    I'm using the generateStreets of type IEnumerator so that I can use a series of nested loops to generate streets. I commented them out at the moment (not shown) for debugging purposes.
     
    tmendez likes this.
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    That type of crash is usually due to an infinite loop somewhere. There's nothing in the code provided that should cause the problem described, so I'd have to assume there's code elsewhere that is to blame.
     
    InfiniteCode22 and nicemiceinice like this.
  3. fieldforensics

    fieldforensics

    Joined:
    Aug 23, 2017
    Posts:
    14
    I temporarily found a fix by merely finding an older version of the code that I made that functioned similarly. However, I've run into an issue where now I'm attempting to instead of generating the world as one, generating it into individual bits/chunks to make the 2D world more infinite. Problem is once again with yield return null. I've checked my code and all the other methods (coroutines) that would be running simultaneously have while loops that yield null as well to prevent an infinite loop that would prevent other methods from running. I've also debugged every coroutine method to where if it exits the while loop and begins to initiate other tasks console would be notified. But pretty much each coroutine is checking to see if the functions (checked by bool statements) have been completed, then it'll exit the while loop and initiate its tasks. Pretty much what all my methods/coroutines look like in the script...
    Code (CSharp):
    1.   IEnumerator create floor()
    2.     {
    3.         while (road == false || trees == false || houses == false)
    4.         {
    5.             yield return null;
    6.         }
    7.         Debug.Log("Beginning Floor Generation!");
    8.         float sizeX = EssentialMethods.getGameObject(Tiles, "TileGrass").GetComponent<SpriteRenderer>().bounds.size.x;
    9.         float sizeY = EssentialMethods.getGameObject(Tiles, "TileGrass").GetComponent<SpriteRenderer>().bounds.size.y; [...]
    In this example, the createFloor algorithm which basically instantiates all the GameObject tiles will only inituate until road, tree, and house algorithms are completed. I know this is a rather strange approach to have the floor/ground algorithm generate after the following however that's just basically how my method for generating has turned out to work best :p

    Here's part of the code for the road algorithm method, error comes from here.
    Code (CSharp):
    1.     private Dictionary<float, string> roads = new Dictionary<float, string>();
    2.     IEnumerator roadGeneration()
    3.     {
    4.         Debug.Log("Beginning Road Generation!");
    5.         float z = -0.0001f; //Simply the z value of which all the road tiles should generate
    6.         //Instantiating values
    7.         GameObject road1 = EssentialMethods.getGameObject(Roads, "Road1");
    8.         GameObject sidewalk = EssentialMethods.getGameObject(Sidewalks, "Sidewalk1");
    9.         float sizeX = road1.GetComponent<SpriteRenderer>().bounds.size.x;
    10.         float sizeY = road1.GetComponent<SpriteRenderer>().bounds.size.y;
    11.         float SideSizeX = sidewalk.GetComponent<SpriteRenderer>().bounds.size.x;
    12.         float SideSizeY = sidewalk.GetComponent<SpriteRenderer>().bounds.size.y;
    13.         float x = this.x;
    14.         float y = EssentialMethods.getRandomNumber(MinimumRoadLengthY + this.y, MaximumRoadLengthY + this.y);
    15.         Debug.Log("Initialized Road Gen!");
    16.         while (x < (Size + this.x)) //Scans all the horizontal tiles
    17.         {
    18.             Debug.Log("Generating road at: " + x + ", " + y);
    19.             GameObject rndRoad = EssentialMethods.getRandomObject(Roads); //Choose a random game object from the GameObject array
    20.             Debug.Log("1");
    21.             if (rndRoad.name != "Road1") //Checks to see if the randomly chosen game is equal to the GameObject name, "Road1".
    22.             {
    23.                 if (!roads.ContainsKey(x))
    24.                 {
    25.                     roads.Add(x, "Intersection");
    26.                     x = RoadGen.generateIntersection(Sidewalks, Roads, x, y); //Calls a method from the RoadGen script. In this case it generates an intersection
    27.                 }
    28.  
    29.             }
    30.             Debug.Log("2"); //returns 2 in console
    31.             yield return null; //error occurs here
    32.             Debug.Log("3");  //never returns 3 in console
     
    Last edited: Feb 11, 2018
  4. tmendez

    tmendez

    Joined:
    Oct 12, 2015
    Posts:
    39
    @fieldforensics I don't suppose you've managed to figure out what went wrong? I am having the same problem! On the `yield return null`.
     
    unity_awUpacKcgHt1UA likes this.