Hi, I am making an RTS where player can select enemies and then my player's units attacks the enemies closest to them. I have a script that handles putting the enemy GameObjects into an ArrayList, but I am having trouble sorting that list in the script attached to each of my player's units.
My code is below:
public void Attack(ArrayList enemyList)
{
//If multiple enemies in list, need to sort the list by distance
if (enemyList.Count > 1)
{
for ( int e = 0; e < enemyList.Count - 1; e ++ )
{
GameObject enemyinlist1 = enemyList[e];
GameObject enemyinlist2 = enemyList[e+1];
float sqrMag1 = (enemyinlist1.position - myTransform.position).sqrMagnitude;
float sqrMag2= (enemyinlist2.position - myTransform.position).sqrMagnitude;
if ( sqrMag2 < sqrMag1 )
{
Transform tempStore = enemyList[e];
enemyList[e] = enemyList[e + 1];
enemyList[e + 1] = tempStore;
e = 0;
}
}
}
}
The specific error I get is with the line "GameObject enemyinlist1 = enemyList[e];" and the error is CS0266:Cannot implicitly convert type 'object' to 'UnityEngine.GameObject'. An explicit conversion exsists (are you missing a cast)?
So it seems that when I pass the ArrayList of GameObjects to the function, the function does not know the ArrayList is full of GameObjects? I tried to solve it by declaring them as GameObjects in the line above, but it is not working. Any thoughts/help is appreciated greatly.
↧