- Home /
How to destroy a unit contained in multiple lists
Hey guys, I have a few lists in my 3D RTS game:
EnemyArray
EnemyList
DistanceToEnemyList
SortedDistanceToEnemyList.
I was wondering if this is the best approach? My problem is that when multiple players are fighting the same enemy, and one of the players kills the enemy (using destroy object), it pings "array out of bounds exception" or that "the object is still trying to be referenced". I have a condition where it clears all the lists after the enemy is dead , but I think the other player unit still thinks its alive at that point. Ive tried putting a bunch of if(blah!=null) as well. Does anyone have another approach or method they have used to solve this problem? Thanks,
EnemyArray is run with an IEnumerator every 0.5 seconds and it detects every enemy object that is spawned in the level. It then adds the object from that array to EnemyList at that index as shown in the code below,
for(int i=0 ; i<EnemyArray.Length; i++){ //iterates through the array and adds all objects to the list
if(EnemyArray[i]!=null){
EnemyList.Insert(i, EnemyArray[i]); //adding to the list
}
}
Next Distance is calculated between the player unit and the enemy unit using a for loop to iterate through the EnemyList. The distance values are stored in DistanceToEnemyList. This list is then sorted as SortedDistanceToEnemyList. Here i take the minimum index [0] as the minimum distance.
for(int i=0; i< EnemyList.Count; i++){
if(EnemyList[i] !=null){
float distance = Vector3.Distance (transform.position, EnemyList[i].transform.position); //finds distance between enemy and player
DistanceToEnemyList.Insert(i,distance); //inserts the distance values at the indicies
SortedDistanceToEnemyList.Insert(i,distance); //this is the same as distancelist but will be sorted
SortedDistanceToEnemyList.Sort();//sorts the list in ascending order
//min distance is the first index of sorted list...
}
}
I then define a variable called minDistanceIndex. Which is the zero index of SortedDistanceList. This will give me the minimum distance value:
minDistanceIndex = DistanceToEnemyList.IndexOf(SortedDistanceToEnemyList[0]);
Answer by Chris_Dlala · Jun 11, 2014 at 09:28 PM
Hi, having so many synced arrays scares me a little - it'll be very hard to ensure they all have the same values at all times. To find the minimum distance I would do a quick search of the entire Array/List:
int closestIndex = 0;
float closestDistance = float.Max;
for(int i = 0; i < EnemyList.Count; i++)
{
// Test for dead enemies
if(!EnemyList[i])
{
// Move on to next
continue;
}
else
{
d = Vector3.Distance (transform.position, EnemyList[i].transform.position);
if(d < closestDistance)
{
closestDistance = d;
closestIndex = i;
}
}
}
Sorting a list does more comparisons than simply finding the current best/closest. This should also gracefully move on and exclude destroyed enemies from the search. Only one list (or array if you prefer) for any number of players accessing it that you can update as often as you like. I hope that helps =)
@Chris_Dlala Hey Chris, THanks for the awesome code. Everything worked, except when the player unit is detecting an enemy it always thinks the enemy with the zero index is the closest enemy and I do not know why.. Do you know why it would do this? Do you think having my list as "public static" in my other class would do this? Thanks,