Unity Freeze On Play

Hello, I am working on a 2d game and for some reason when I press play it freezes. It takes up more and more memory until I go into task manager, and shut it down. All this started when I tried to make my own world generation script. I fully realize this might be because of an endless loop, but I looked through my code and could not find one. Any help would be appreciated.

Generation Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WorldGeneration : MonoBehaviour
{

    public GameObject[] forestGeneration;
    public int genType;
    public float chance;
    public List<Transform> generationSpots;
    public List<Transform> previousGenerationSpots;

    public float minX;
    public float maxX;
    public float minY;
    public float maxY;

    public float minSpaceApart;

    public GenType generationType;
    public Vector2 genOffset;

    public bool firstGen;
    public bool secondGen;
    public bool done;
    public List<bool> theDistance;
    List<bool> blank;

    public GameObject worldObject;

    private void Start()
    {

        firstGen = true;
        genType = generationType.generationType;

        if(genType == 1)
        {

            float boolnum;
            minSpaceApart = 3;
            chance = 0.01f;
            forestGeneration = generationType.genForest;

            for (int i = 0; i < 1000 * chance; i++)
            {

                boolnum = 0;
                GameObject spawnSpot = Instantiate(worldObject, new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY)), Quaternion.identity);
                generationSpots.Add(spawnSpot.transform);
                Vector2 position = new Vector2(spawnSpot.transform.position.x, spawnSpot.transform.position.y);

                if(previousGenerationSpots.Count > 0 && !firstGen)
                {

                    done = false;

                    for (int c = 0; c < previousGenerationSpots.Count; c++)
                    {

                        Vector2 pastPosition = previousGenerationSpots[c].position;
                        float distance = Vector2.Distance(position, pastPosition);

                        Debug.Log(distance);

                        if (secondGen)
                        {

                            if(Vector2.Distance(previousGenerationSpots[c].position, position) > minSpaceApart)
                            {

                                theDistance.Add(true);
                                boolnum++;

                            } else {

                                theDistance.Add(false);

                            }

                        } else {

                            if (Vector2.Distance(previousGenerationSpots[c].position, position) > minSpaceApart && !secondGen)
                            {

                                if(theDistance.Count < c + 2)
                                {

                                    theDistance.Add(true);
                                    boolnum++;

                                } else {

                                    theDistance[c] = true;
                                    boolnum++;

                                }

                            } else if (Vector2.Distance(previousGenerationSpots[c].position, position) > minSpaceApart) {

                                if (theDistance.Count < c + 2)
                                {

                                    theDistance.Add(false);

                                }
                                else
                                {

                                    theDistance[c] = false;

                                }

                            }

                        }

                        if (c == previousGenerationSpots.Count - 1)
                        {

                            secondGen = false;
                            done = true;

                            if (done == true && boolnum == theDistance.Count)
                            {

                                done = false;
                                previousGenerationSpots.Add(generationSpots[i]);
                                GameObject spawn = Instantiate(forestGeneration[Random.Range(0, forestGeneration.Length)], generationSpots[1].position, Quaternion.identity);
                                spawn.transform.parent = spawnSpot.transform;

                            }

                        }

                    }

                } else {

                    if (firstGen)
                    {

                        secondGen = true;
                        firstGen = false;
                        previousGenerationSpots.Add(generationSpots[i]);
                        Vector2 offsetPos = new Vector2(generationSpots[i].position.x - 8, generationSpots[i].position.y - 8);
                        GameObject spawn = Instantiate(forestGeneration[Random.Range(0, forestGeneration.Length)], offsetPos, Quaternion.identity);
                        spawn.transform.parent = spawnSpot.transform;

                    }

                }

            }

        }
    

    }

}

Generation Type Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenType : MonoBehaviour
{

    public int generationType;
    public string Forest = "1";
    public string Plain = "2";

    public GameObject[] genForest;

}

Any Ideas to increase Performance?

I had a similar issue recently but with a much simpler loop than included the “equal or lesser than” operator. My suggestion would be adding an exception throw at each loop that you have and have a clear indication of each loop working or not working succesfully in the console (how to structure your Debug.Log messages is up to you).
My initial guess would be that your issues are in the line 46.

The first thing to do would be turn this into an IEnumerator that you start with StartCoroutine, add a yield return null; to every iteration of the loop. That way, no matter how long it’s taking, you can watch it work, interrupt it, etc. Once you can do that, it’ll be much easier to iterate on performance and test your changes. (It’ll also be a better user experience, because you can run this generation in the background while the user does other stuff)

1 Like

Well, it is likely an endless loop. In Visual Studio, you can use the debugger. Add some breakpoints and use the Attach to Unity. This way you can step through the code and view variable values and such.

7412672--906539--upload_2021-8-12_10-43-28.png

The other option is Debug.Break will let you pause so you can check values.

I haven’t had the chance to use it, but I’ve seen it mentioned a few times.

@StarManta idea of a coroutine is also a good idea.