Unity Crashing when this function is called.

I have a button that runs this function.

public void Generate()
    {
        laserPos = new Vector3(-terrainSize, 500, -terrainSize);
        Quaternion q = new Quaternion();
        q.eulerAngles = new Vector3(-90, 0);
        randTransform.rotation = q;
        int distanceToCover = 100 / densityPer100Meters;
        x = 0;
        int z = -terrainSize;
        i = 0;
        begin:
        while (x < terrainSize + 1)
        {
            Invoke("XGen", 5.1f);
            laserPos = new Vector3(x, 500, z);
        }
        if (x == terrainSize && z < terrainSize)
        {
            z += 1;
            x = 0;
            goto begin;
        }
        if (x == terrainSize && z == terrainSize)
        {
            Debug.Log("RGE is finished.");
        }
    
    }

    public void XGen ()
    {
        
        int distanceToCover = 100 / densityPer100Meters;
        RaycastHit hit;
        if (Physics.Raycast(laserPos, randTransform.forward, out hit, 500))
        {
            if (hit.transform.tag == "Terrain")
            {
                int rx = Random.Range(-5, 5);
                int rz = Random.Range(-5, 5);
                Quaternion rq = new Quaternion();
                Vector3 placeHolder = new Vector3(0, Random.Range(-179, 180), 0);
                rq.eulerAngles = placeHolder;
                Vector3 spawnPos = hit.point;
                spawnPos.x += rx;
                spawnPos.z += rz;
                int rPrefab = Random.Range(0, spawnablePrefabs.Length + 1);
                spawns *= Instantiate(spawnablePrefabs[rPrefab], spawnPos, rq);*

x += distanceToCover;

}
else
{
x += distanceToCover;
}
}

}
It’s meant to spawn in a prefab from an array in a location on the map. When I click th UI button to call this function, Unity freezes and becomes unresponsive. Does anyone know the cause of this or how to fix it.

This snippet:

     begin:
     while (x < terrainSize + 1)
     {
         Invoke("XGen", 5.1f);
         laserPos = new Vector3(x, 500, z);
     }
     if (x == terrainSize && z < terrainSize)
     {
         z += 1;
         x = 0;
         goto begin;
     }

Suggests an infinite loop. What would change x so that it is < terrainSize + 1? XGen? Where in XGen is that certain to happen?


If if that does happen, does it fire the next if? You’ve used a goto begin; there which could define a secondary infinite loop. These are the most likely places in which such a loop would completely lock Unity at runtime.


There is a long running philosophy about how ‘goto’ is always a bad idea, and as a veteran of 30 years in programming in a number of languages, I have to agree. ‘goto’ can cause all manner of issues without any good need. Every bracket, every loop is an implied goto, which makes the keyword ‘goto’ unnecessary. What you have is more logical to fashion as nested while loops, or perhaps a do while with a nested while.


From what I read, the begin label is, in reality, what the code should be doing while x == terrainSize && z < terrainSize, and while I understand you reason for using Invoke, I suspect the performance costs are too high to be a good solution here. It may be better to prepare data to forward to Update for calling Instantiate on a completed set of data. This version makes the (I assume threaded) Generate function waste it’s potential waiting on the Invoke.