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]--;
}
}
}
}
}