The index in playerBattlers
and enemyBattlers
are not necessarily the same in allBattlers
. allBattlers
could have (Enemy, Enemy, Player, Enemy, Player). If you pick indices from enemyBattlers
, your list would be (0, 1, 2) instead of (0, 1, 3), and picking indices from playerBattlers
would be (0, 1) instead of (2, 4). The random selection will return an index from either enemyBattlers
or playerBattlers
for use in allBattlers
which will be the wrong index.
I have not been following the course yet - just looked at this lecture quickly - so I’m not sure why the two separate lists were made. Also not sure if they’re being kept up to date. If an enemy dies, does the enemyBattlers
get updated? Same for playerBattlers
. If so, then I see no reason why you can’t do what you did and then use the index to pick from the correct list, i.e. if you picked a random enemy from the (0, 1, 2) index list, use the index on enemyBattlers
instead of allBattlers
. Again, same for playerBattlers
; if you picked an index from the (0, 1) list, use that index on the playerBattlers
list instead of the allBattlers
one.
If that is indeed the case, there’s also no need to do a for loop. You can just pick a random number from 0 to count
private int PickRandomEnemy()
{
return Random.Range(0, enemyBattlers.Count);
}
private int PickRandomPlayer()
{
return Random.Range(0, playerBattlers.Count);
}
I would personally also name the functions PickRandomEnemyIndex()
and PickRandomPlayerIndex()
because we’re not picking enemies or players, we’re picking their indices. But that’s just me