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?