Missing Reference Exception
Hi, I'm having an issue with my enemy lists where if a group is empty it will still run through the for loop causing a missing reference exception and not spawning a new group. I think it's not removing the empty list but I can't seem to figure out why it isn't.
I wasn't having this issue before when I had the for loop as
for(int j = enemyGroupList.Count - 1; i>=0; i--)
This was throwing an argument out of range exception and wasn't what I needed there but it would update the groups properly and spawn a new one when needed.
void Update () {
if(enemyGroupList.Count < groupNumber)
{
enemyGroupList.Add(createGroup());
}
// for each list in the group list
for(int i = enemyGroupList.Count - 1; i >= 0; i--)
{
// for each opject in a given list
for (int j = enemyGroupList[i].Count - 1; j >= 0; j--)
{
// pause and unpause enemies when the player pauses the game or is talking
if (player.CurrState == PlayerScript.State.Paused || player.CurrState == PlayerScript.State.Talking)
{
enemyGroupList[i][j].enabled = false;
}
else
{
enemyGroupList[i][j].enabled = true;
}
// remove dead enemies
if (enemyGroupList[i][j].Dead)
{
enemyGroupList[i].RemoveAt(j);
// break avoids out of range exceptions
}
}
if (enemyGroupList[i].Count == 0 || player.CurrState == PlayerScript.State.Win)
{
enemyGroupList.RemoveAt(i);
}
}
}
Thanks
Comment
Your answer
Follow this Question
Related Questions
Can only be called from the main thread 2 Answers
How to create a 30 min demo scene? 0 Answers
C# data saved in playerPrefs with Java menu 0 Answers
How can I create animation keyframes using C# scripting? 0 Answers
what am i doing wrong? 1 Answer