List().Add freezes Unity
I have a method that I try call and it just freezes when it reaches the biggestTiles.Add (list [x, y]);
. I've tried eliminating every single line, and I'm pretty sure that's the cause of the freeze. It doesn't seem to have a problem with adding it to a completely new List of GameObjects inside the loops, but the biggestTiles List just doesn't seem to like that specific GameObject, or something. The tiles are not null or destroyed. And I can't even debug the error since Unity hangs before anything can be printed. Commenting those lines makes the game run fine, but without the intended functionality.
public void ChooseWeakPoint(GameObject[,] list){
//Loop trough tiles to check for highest elapsedToBuff
var biggestTiles = new List<GameObject>();
float biggestValue = 0f;
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 3; y++) {
var comp = list [x, y].GetComponent<BattleButton> ();
if(comp.buffCoroutine == null && comp.elapsedToBuff > biggestValue && comp.ButtonState != ButtonStates.Disabled && comp.ButtonState != ButtonStates.Occupied ){
biggestValue = comp.elapsedToBuff;
biggestTiles.Clear();
//biggestTiles = new List<GameObject>(); // Tryed using instead of Clear(). Still doesn't work.
//var newList = new List<GameObject> (); // Creating a new list inside the loop seems to work
//newList.Add (list [x, y]); // It added the GameObject with no problem.
//biggestTiles.AddRange (newList); // But when trying to add it to biggestTiles, it freezes Unity.
biggestTiles.Add (list [x, y]); // THIS FREEZES UNITY
} else if (comp.elapsedToBuff == biggestValue) {
biggestTiles.Add (list [x, y]); // THIS ALSO FREEZES UNITY
}
}
}
//Choose a tile from the selected biggestTiles
if(biggestTiles.Count > 0){
int rand = Random.Range (0, biggestTiles.Count);
GameObject chosenTile = biggestTiles [rand];
chosenTile.GetComponent<BattleButton> ().elapsedToBuff = -999;
chosenTile.GetComponent<BattleButton> ().FadeTileForBuff (3f,ElementTypes.WeakPointBuff);
}
}
I know this is an old thread, but I seem to be having the exact same problem. I've reached out to a few different dev communities and nobody seems to know what's going on.
Your answer
Follow this Question
Related Questions
null referance when adding to a list please help 1 Answer
Get a selected item from a GameObject list? 0 Answers
Add inactive objects to list 0 Answers
Insert object to the list in the script in real-time. 1 Answer
What range of values need to be inserted if we need to pull a random item from the list? 0 Answers