Trying to add a list in void Update() but crashes

I’m running a big code but the below section seems to cause Unity to crash. I don’t know if it’s in conjunction with the rest of the code. I still have to fix the rest of the code and am new at programming but the crash makes it very hard to trouble shoot.

        //Sets all items totalItemsInRow to 0.
        if (totalItemsInRow.Count > 0)
        {
            totalItemsInRow.Clear();
        }

        if (totalItemsInRow.Count == 0)
        {
            for (int i = 0; i < layerBools.Count; i++)
            {
                totalItemsInRow.Add(0);
            }
        }

Here is the rest of the code in the void Update:

    private GameObject RTSCamera;
    public List<GameObject> buildersBuildings;
    private List<GameObject> factionButtons;
    private List<bool> layerBools;
    private List<int> layerNames;
    private Vector3 iconPos;
    private int boolListCounter = 0;
    private int totalLayers;
    private int iconLayer;
    private bool buildMenuIsUp = false;
    [HideInInspector]
    public List<int> totalItemsInRow;
 void Update()
    {
        //Displays the build tooltips.
        if (this.GetComponent<UnitAI>() != null && this.GetComponent<UnitAI>().UnitOrBuildingIsSelected == true && buildMenuIsUp == false)
        {
            RTSCamera.GetComponent<ButtonList>().genericButtons.Find(obj => obj.name == "BuildingTooltip").SetActive(true);
            RTSCamera.GetComponent<ButtonList>().genericButtons.Find(obj => obj.name == "BuildButton").SetActive(true);
        }
        else if (this.GetComponent<UnitAI>() != null && this.GetComponent<UnitAI>().UnitOrBuildingIsSelected == false || buildMenuIsUp == true)
        {
            RTSCamera.GetComponent<ButtonList>().genericButtons.Find(obj => obj.name == "BuildingTooltip").SetActive(false);
            RTSCamera.GetComponent<ButtonList>().genericButtons.Find(obj => obj.name == "BuildButton").SetActive(false);
        }

        //Goes through the list of available buildings to build and activates their icons and turns off the generic building tooltips.
        if (Input.GetKeyDown(KeyCode.B) && this.GetComponent<UnitAI>() != null && this.GetComponent<UnitAI>().UnitOrBuildingIsSelected == true)
        {
            buildMenuIsUp = true;

            //Turns off any building icons that might be turned on.
            foreach (GameObject i in factionButtons)
            {
                if (i.activeInHierarchy == true)
                {
                    i.SetActive(false);
                }
            }

            //Turns on corresponding building icons.
            foreach (GameObject i in buildersBuildings)
            {
                foreach (GameObject o in factionButtons)
                {
                    if (i.name + "Button" == o.name)
                    {
                        o.SetActive(true);
                        break;
                    }
                }
            }

            //Sets all items totalItemsInRow to 0.
            if (totalItemsInRow.Count > 0)
            {
                totalItemsInRow.Clear();
            }

            if (totalItemsInRow.Count == 0)
            {
                for (int i = 0; i < layerBools.Count; i++)
                {
                    totalItemsInRow.Add(0);
                }
            }

            //Sets what layers are active, lower number layers are at the bottom. FIX ITEMS IN ROW!!!!!
            foreach (GameObject i in factionButtons)
            {
                int multipleAdjacentLayersGrouperBreaker = 1;
                Debug.Log("Step One.");
                if (i.activeInHierarchy == true)
                {
                    iconLayer = i.GetComponent<IconRow>().layer;
                    //layerBools is used to determine how many layers are needed to evenly space the buildings.
                    foreach (int o in layerNames)
                    {
                        for (int p = 0; p < layerBools.Count;)
                        {
                            //If an entity has the same layer number as the layerbreaker, it keeps adding items to the row.
                            if (iconLayer == o && o == multipleAdjacentLayersGrouperBreaker)
                            {
                                layerBools[p] = true;
                                totalItemsInRow[p] += 1;
                                Debug.Log("Item added that's same layer.");
                                break;
                            }
                            //If an entity has a different layer number than the layerbreaker, it adds a new row.
                            else if(iconLayer == o)
                            {
                                multipleAdjacentLayersGrouperBreaker++;
                                p++;
                                layerBools[p] = true;
                                totalItemsInRow[p] += 1;
                                Debug.Log("Item added that's different layer.");
                                break;
                            }
                            //If an entity doesn't match the bool layer it is skipped.
                            else
                            {
                                layerBools[p] = false;
                                p++;
                                Debug.Log("Skipped this item.");
                            }
                        }
                    }
                }
            }

            //Finds how many levels there are.
            foreach (bool o in layerBools)
            {
                if (o == true)
                {
                    boolListCounter ++;
                }
            }

            totalLayers = layerBools.Count - boolListCounter;
            
            //Sets the buttons in the correct spots. Fix this spot!!!
            foreach (GameObject i in factionButtons)
            {
                if (i.activeInHierarchy == true)
                {
                    for (int o = 0; o < totalItemsInRow.Count;)
                    {
                        iconLayer = i.GetComponent<IconRow>().layer;
                        while (iconLayer > totalLayers)
                        {
                            iconLayer--;
                        }
                        iconPos.y = -223 + (70 * (iconLayer - 1));
                        Debug.Log(iconPos.y);
                        iconPos.x = 510 - (70 * (totalItemsInRow[o] - 1));
                        Debug.Log(iconPos.x);
                        iconPos.z = 0;
                        i.transform.position = iconPos;
                        totalItemsInRow[o]--;
                    }
                }
            }
        }

    }

If the editor is crashing, there should be a stacktrace in the Editor.log which would point to what is causing the crash
Post the stacktrace here : )

Well fix that first because if YOU don’t know it, imagine how in the dark we are!!

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Don’t move onto new code until the old code functions perfectly. Otherwise you’re building a castle on wet sand.

As for the lockup, one of your exit conditions is likely not being met. Use debugging to find out!

Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update.

Absolutely NOTHING will happen… until your code either:

  • returns from whatever function it is running

  • yields from whatever coroutine it is running

As long as your code is looping, Unity isn’t going to do even a single frame of change. Nothing.

No exceptions.

“Yield early, yield often, yield like your game depends on it… it does!” - Kurt Dekker

Thank you so much! I didn’t even know that existed, the entirety of Unity would crash each time and I got frustrated I couldn’t read the Console to see what error it was. It looks like the line I thought didn’t work actually works. It’s looking like I accidently created an infinite loop at the bottom of the code causing Unity to get overburdened or something.

Thank you for your insight, I definitely made a mistake of approaching the end of my code and not checking some of the functions I was adding. The biggest hiccup was I didn’t know you can access the editor.log after Unity would crash so I couldn’t check the console for my newest debug.Logs. I think you’re right and one or more of the exit conditions at the bottom of my code might not be being met. Thanks again for your help, I’ve never experienced this before.

It’s not really burden. Unity gives your scripts 100% control. If your scripts don’t finish and return (or yield), then your game never continues. It is locked solid.