- Home /
Remove and resize List
I have all my player transforms in a list. And I want to remove one player. I can do that by using Remove.
void OnNetworkDisconnect ()
{
gs.allPlayers.Remove (_playerTransform);
gs.UpdatePlayerLists ();
Debug.Log ("removing player");
Application.LoadLevel("NetworkStart");
}
However, once I remove the player how do I resize the list back to just the remaining players. By removing the player, his transform is removed, but the list is the same size as in the following pic:
I attempted to go through list and set it equal to a temp list but it doesnt work.
public void UpdatePlayerLists() {
System.Collections.Generic.List<Transform> allPlayersTemp = new System.Collections.Generic.List<Transform>();
int loop = 0;
for (loop = 0; loop < allPlayers.Count; loop++) {
if (allPlayers[loop] != null)
allPlayersTemp.Add (allPlayers[loop]);
}
allPlayers = new System.Collections.Generic.List<Transform> ();
allPlayers = allPlayersTemp;
}
Anyone have thoughts on how I can remove a transform from a list and resize the list to the remaining values.
Thanks
Hmm. $$anonymous$$aybe its not getting called properly. I didn't write a very good question. Thanks!
Note that you'll want to remove it from the list before its destroyed. Or perhaps in OnDestroy.
Answer by kdubnz · Nov 22, 2014 at 07:52 AM
voncarp,
The List should not require resizing. This can be demonstrated with a simple console App.
static void Main(string[] args)
{
List<string> players = new List<string>();
players.Add("alpha");
players.Add("bravo");
players.Add("charlie");
players.Add("delta");
players.Add("echo");
Console.WriteLine("\nCapacity: {0}", players.Capacity);
Console.WriteLine("Count: {0}", players.Count);
foreach (var player in players)
{
Console.WriteLine(player);
}
Console.WriteLine( );
players.Remove("delta");
Console.WriteLine("\nCapacity: {0}", players.Capacity);
Console.WriteLine("Count: {0}", players.Count);
foreach (var player in players)
{
Console.WriteLine(player);
}
Console.WriteLine();
players.TrimExcess();
Console.WriteLine("\nCapacity: {0}", players.Capacity);
Console.WriteLine("Count: {0}", players.Count);
}
Which returns :
Capacity: 8 Count: 5 alpha bravo charlie delta echo
Capacity: 8 Count: 4 alpha bravo charlie echo
Capacity: 4 Count: 4
Perhaps you could add some code to log to the console the Count and print the list content before and after the List.Remove()
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Remove and Add to List By Name aad 1 Answer
How can I remove an item ffrom a list. 1 Answer
Find specific prefab in a list 2 Answers
Removing Inputs from the Input list 1 Answer