Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by wolga2 · Sep 03, 2013 at 05:29 PM · listremoveclear

Clear whole list

Hi! Is there a possibility to clear a whole list (the index entries and the objects) with just a simple command, so that the list will be empty and the size of the elements will be 0 again, and the objects distroyed? Something like Clear() or RemoveAll()?

I have this code so far, but isn't there a simpler way to do it that takes less time?

 using System.Linq;
 using System.Collections.Generic;


 public List <GameObject> WP;


 WP = GameObject.FindGameObjectsWithTag("waypoints").OrderBy( go => go.name ).ToList();
 
 
 foreach (GameObject goWp in WP)
 {
     Destroy (goWp);
 }

 for (var i = 0; i < WP.Count; i++)
 {
     WP.RemoveAt(i);
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by DESTRUKTORR · Sep 03, 2013 at 05:34 PM

The list clear method simply loops through and clears out all the entries for the list, same as this does. Unless your list is massive in size, clearing out basic objects from it shouldn't take that much time.

On another note, why is it you need to destroy the game objects? Destroying game objects is quite costly, and is often suggested against.

I usually just make a linked list "buffer" of game objects with the components I need them to have and enable/disable them, dynamically by simply pushing and popping them off, like a stack, disabling the game object when it's pushed to the stack, and re-enabling them when they're popped off.

While various methods will often make it seem as though collections can be cleared/created with one command, the methods themselves will still use looping or recursion to execute the exact same thing, as it's impossible to do it any other way, really.

Comment
Add comment · Show 2 · 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
avatar image wolga2 · Oct 01, 2013 at 10:36 PM 0
Share

$$anonymous$$y actually problem was, that I could not use the List any longer. So I finally could solve the problem simply by Clear() the list and then - if used - just say mayList = new List . Thanks anyway!

avatar image thunderbuns · Dec 03, 2018 at 05:55 AM 0
Share

Talk about going above and beyond when answering a question.

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

20 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

Related Questions

Can't remove/replace/clear list items 0 Answers

A node in a childnode? 1 Answer

Removing from a list 1 Answer

Remove and resize List 1 Answer

How do I remove a specific item from a list?,How would I remove a specific item in a list? 2 Answers


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