My Project crashes each time this script is run

This script crashes each time it is run, this only started occurring after I added a check to not create new tiles over ones that already were created. I hope anyone knows what’s going on.

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class GenerateEnviroment : MonoBehaviour
{

    public float curX = 5f;
    public float curZ = 5f;

    float xQuad;
    float zQuad;
    
    public float curWithinX = 0f;
    public float curWithinZ = 0f;

    public float curTileX;
    public float curTileZ;

    bool cando = true;
    float mult = 1f;

    public List<List<int>> createdTiles = new List<List<int>>();

    void Start() {
        createdTiles.Add( new List<int>{0, 0} );
    }

    // Update is called once per frame
    void Update() {
        // Get Position
        curX = transform.position.x + 5;
        curZ = transform.position.z + 5;

        // Current Tile and Where
        curTileX = Mathf.Floor(curX / 10f)*10f;
        curWithinX = curX + (Mathf.Floor(curX / 10f)*10f);
        curTileZ = Mathf.Floor(curZ / 10f)*10f;
        curWithinZ =  curZ + (Mathf.Floor(curZ / 10f)*10f);

        // Spacebar
        if (Input.GetKey(KeyCode.Space) && cando == true) {
            cando = false;
            for (int xCycle = -1; xCycle < 2; xCycle++) {
                for (int zCycle = -1; zCycle < 2; zCycle++) {

                    List<int> currentTiles = new List<int>{xCycle, zCycle};
                    for (int b = 0; b < createdTiles.Count; b++) {
                        Debug.Log("point");
                        if (createdTiles[b] != currentTiles) {
                            Debug.Log("create");
                            GameObject tile = Instantiate(GameObject.Find("Prefabs/GroundTile"), new Vector3((curTileX*mult) + xCycle*10f, 0, (curTileZ*mult) + zCycle*10f), Quaternion.identity);
                            tile.name = "tile" + (xCycle+curX) + "," + (zCycle+curZ);
                            createdTiles.Add( new List<int>{xCycle, zCycle} );
                        }
                    }
                    
                }
            }
        }
        if (Input.GetKey(KeyCode.O)) {cando = true;}
    }
}

What are these things you are trying to debug?
Does the script still crash if you remove them?
Debug.Log is awfully resources hungry. You should find another way to get the information you are looking for. :slightly_smiling_face:

“Crash” is too vague. We need more information here. Does Unity close? Does it hang indefinitely? Does it run into an exception?

I’m not trying to start an argument but do you have a) anything that indicates that Debug.log is a problem and b) what do you use as an alternative?

I use it (via my own Logger class) thousands of times in any app I write.

Edit: Ah, I assume that you mean in the Update method given how often it can be called. In which case, yes that is a pain if for no other reason than it can generate a whole lot of log lines. Personally I have a conditional logging method in my Logger class and that helps considerably in methods like Update if you only occasionally need to inspect a value.

There are more than a few questionable practices (mostly inconsistencies) but the following looks odd. Can they ever be equal?

if (createdTiles[b] != currentTiles)

You also have a lot of “magic numbers” I assume relate to the dimensions of the tile but the code doesn’t say that it simply references 5 and 10, most often as a float but sometimes not.

Personally I recommend moving the code out of the Update method to one or more methods named according to what they do. Make the methods callable directly and Update can call them AND you can call them when testing (for instance) the calculations of the current tile.

When debugging code it is important to check all the values. It is easy to assume a formula is working but better to confirm it.

There is a lot to fix in your code. Why do you store points as a list?

You have an infinite loop

You’re adding to a list while you’re processing it.

Most likely you’re adding to it fast enough that you never get through it.

Comment out line 56 above and it shouldn’t hang anymore.

Now go reason about what it is you’re doing and why you feel compelled to both iterate a list AND modify it. Perhaps add the new items to ANOTHER list, then tack them on afterwards with a simple .AddRange() call??

3 Likes