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
0
Question by choijaeyoung · Jan 19, 2021 at 06:03 PM · class instance

Any way to destroy internal classes that were instantiated?

I'm building a script that will instantiate some internal script when called. As in,

 public class Example : MonoBehaviour
 {
    private List<NewClass> newClassList = new List<NewClass>();
   
    public void MakeNewClass()
    {
       newClassList.Add(new SomeClass());
    }

    public void Activate()
    {
       foreach (NewClass class in newClassList) { class.DoSomething(); }
    }
 }
   
 internal class SomeClass : NewClass
 {
    // does something
 }

Now, this function worked just fine... but the thing is, I worry that that would be best practice to do so.

I am creating new class every time NewClass is created via new SomeClass(), but I never get to unload? or destroy? them. Whatever the correct term for that is. I am creating more and more classes without ever removing them, so I worry that this might cause some issue in the future.

Is there anyway I can destroy internal classes? I can't seem to find a way to do that.

Comment
Add comment · Show 2
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 Llama_w_2Ls · Jan 19, 2021 at 06:45 PM 0
Share

I'm not actually entirely sure how C# handles problems like these, as classes don't have destructors. Normally, in C++, you were given the freedom to decide whether to deload your class or not. I'm guessing in C#, it does it automatically for you, if it sees that it isn't being used. I don't know for sure. $$anonymous$$aybe someone else can help, but I wouldn't worry about it for now. If you do get a problem, then I'm sure someone else has already had that issue, and someone has provided a solution for that.

avatar image logicandchaos Llama_w_2Ls · Jan 19, 2021 at 07:33 PM 0
Share

C# uses garbage collection.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by logicandchaos · Jan 19, 2021 at 07:36 PM

You are talking about a stack overflow, when memory on the stack exceeds available memory. In managed languages like C++ you have to manage memory yourself, C# uses garbage collection, so whatever isn't being used is removed from memory by that. If you want to 'destroy' an instance of a class then set it to null, that should make the GC dispose of it in memory. This is my 250th answer! Thanks!

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

112 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 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 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 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

Why is my MonoBehaviour never being deleted? 0 Answers

Object reference not set to an instance of an object (singleton) 1 Answer

Array of Objects of a Class 1 Answer

Loading class instances from Json file gives ArgumentNullException 1 Answer

Is there anyway to disable clients GameObject instance from Server or Vice-versa? 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