Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by voncarp · Nov 22, 2014 at 05:39 AM · listresizeremove

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:

alt text

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Nov 22, 2014 at 05:55 AM 2
Share

??? List.Remove automatically resizes the list.

avatar image voncarp · Nov 22, 2014 at 06:18 AM 0
Share

Hmm. $$anonymous$$aybe its not getting called properly. I didn't write a very good question. Thanks!

avatar image Kiwasi · Nov 22, 2014 at 07:22 AM 0
Share

Note that you'll want to remove it from the list before its destroyed. Or perhaps in OnDestroy.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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()

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges