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 ungalyant2 · Aug 14, 2013 at 10:53 AM · listsclassesmemory-leak

Cleaning Up Classes In Lists

I've had a concern for a while, and from searching around I haven't been able to find a definite answer. When making a list of any given class, by simply removing that class from the list, within Unity, does that inherently remove the class from memory, or are there further steps that need to be taken to make sure this happens? Normally such a thing would lead to a memory leak, but I haven't yet had this problem with Unity (from what I can tell) but it makes me a little cautious when I don't know.

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
0

Answer by fafase · Aug 14, 2013 at 11:02 AM

Removing an item from a list only remove the reference to the item contained in the list. A list does not hold the object but just a reference to it.

As a result, when doing:

 list.RemoveAt(index);

the object is still in memory and could still be retrieved with GameObject.Find("Name").

This below will remove the object on collision:

 void OnCollisionEnter(Collision col)
 {
    if(col.gameObject.tag == "Projectile"){
       projectileList.Remove(col.gameObject);
       Destroy(col.gameObject);
    }
 }

That considers you are storing your projectile in a list right after you instantiate them. This will remove from the list and destroy the object. Later on garbage collector will free the memory when needed.

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 ungalyant2 · Aug 14, 2013 at 11:05 AM 0
Share

I don't specifically mean game objects that are in the heirarchy, I mean a list of classes that is held purely as information. Not game objects. Is the rule the same?

avatar image fafase · Aug 14, 2013 at 11:17 AM 1
Share

oh you mean removing a script from an object. That cancels the creation of the instance of the object yes.

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

15 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

Related Questions

How to search a certain variable in a list of a created class (C#) 1 Answer

Trying to make a simple quest system, but i stuck how to do 0 Answers

Save an array with custom classes 1 Answer

Add Item To A List Of Classes 0 Answers

Lists within lists to make a crafting system 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