I'm coding a bot for a game, in which several players have different actions and use them to interact with each other.
One of the actions I'm coding chooses 2 players (A and B), and makes every other player that was targeting A, target B instead, and vice versa.
However, with my code, only players targeting B have their target swapped. If a player is targeting A initially, my code changes it to B and then changes it to A back.
var target1 = Players.FirstOrDefault(x => x.Id == doorman.Choice);
var target2 = Players.FirstOrDefault(x => x.Id == doorman.Choice2);
if (target1 != null && target2 != null)
{
var going1 = Players.Where(x => x.Choice == target1.Id && x.Id != doorman.Id);
var going2 = Players.Where(x => x.Choice == target2.Id && x.Id != doorman.Id);
foreach (var visitor in going1)
visitor.Choice = target2.Id;
foreach (var visitor in going2)
visitor.Choice = target1.Id;
}
I think the problem is that the list going2
gets updated even after being declared, and has all the players in going1
added to it.
This means all players that are targeting target1
are swapped to target2
and then back to target1
.
How can I make my going1
and going2
lists not update by themselves after being declared?