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.