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
1
Question by Seth-McCumber · Dec 05, 2013 at 08:25 PM · gameobjectlistrandom

Reset Generic List To Empty(C#, Urgent!)

I need to reset a generic list(You know, from the System.Collections.Generic namespace) to a new list, bassicaly like its never been touched, entirely new. This is simply because when i choose a random gameobject from the list, it is sometimes a null gameobject, thus meaning that the rest of the script that does things with that gameobject does nothing. Thanks!

I really need an answer quick, so please if you have any idea tell me!

Comment
Add comment · Show 1
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 clunk47 · Dec 05, 2013 at 08:29 PM 1
Share
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Example : $$anonymous$$onoBehaviour 
 {
     List<string> testList = new List<string>();
 
     void Start()
     {
         string[] temp = new string[]{"1", "2", "3", "4"};
         testList.AddRange(temp);
         print (testList.Count);
     }
 
     //TEST
     void Update()
     {
         if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.C) && testList.Count > 0)
         {
             testList.Clear();
             print (testList.Count);
         }
     }
 }
 

2 Replies

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

Answer by Spinnernicholas · Dec 05, 2013 at 08:28 PM

 ListName.Clear();
 //or
 ListName = new List<T>();
Comment
Add comment · Show 4 · 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 Seth-McCumber · Dec 05, 2013 at 08:43 PM 0
Share

(facepalm) So simple, how did i miss that?!

avatar image Spinnernicholas · Dec 05, 2013 at 08:48 PM 0
Share

Lol, it happens to the best of us.

avatar image vexe · Dec 06, 2013 at 04:31 AM 4
Share

I happened to answer a similar question recently - But @fafase re$$anonymous$$ded me of something - is that it's important to watch out not just to clear the list if you had a list of "Objects" (Components, GameObjects, etc) - basically anything that could be instantiated in the Unity scene - if so, then you have to Destroy them as well, not just clearing the list that references them (ie a list of Enemies, Cubes, etc). If you don't do that and just clear the list, you'll end up with a memory leak if there's nobody else beside the list that references them.

avatar image Skuxxnosch vexe · Mar 26, 2020 at 01:02 PM 0
Share

You will not create a memory leak by doing what you said. One does not need to worry about memory leaks because you dont allocate and release memory in average Unity program$$anonymous$$g.

If there is an object that is only referenced in a list and you delete that reference by list.clear() or something like list = new List<T>() then the garbace collection will take care of unreferenced objects. To learn more about garbage collection go here.

avatar image
0

Answer by Skuxxnosch · Mar 26, 2020 at 02:10 PM

Be aware that list.clear(); and list = new List<T>(); are not the same.

When you are asking for a "new list, bassicaly like its never been touched, entirely new" , then you should use list = new List<T>(); since it should just reference a newly allocated List with list.count and list.capacity being 0. The now referenceless list is going to be garbage collected if not referenced somewhere else.

When you use list.clear(), it will remove all references of the list entries resulting in a list.count == 0 . But the list.capacity will remain the same as during the usage before (this is crucial for extremely long and memory intensive lists). The capacity is basically the space the list has for possible entries. If you want to have list.capacity == 0 , use list.TrimExcess(); . Useful links: List, Capacity

Comment
Add comment · Show 1 · 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 Spinnernicholas · Mar 26, 2020 at 09:37 PM 0
Share

You have a good point.

I would say that in most case, people are going to use the list for the same task and therefore need a similar capacity. If that's the case, they should probably use list.Clear(); and save some cycles from the list expanding repeatedly.

But it would be a waste of memory to build a list of thousands of items, clear it and then only use it for a couple items. In that case, they should just create a new list.

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

21 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

Related Questions

Instantiate random from a list gives error: "Reference not set to an instance" 1 Answer

How to randomly iterate through a list of objects without repeating? 3 Answers

A node in a childnode? 1 Answer

How i generate Gameobjects list? 0 Answers

Create a null GameObject List using Boo 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