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 NoUJoe · Jun 11, 2015 at 05:55 PM · c#destroynulldestroy object

Destroy () vs nulling an object created in script - c#

So if I created a new material from within a script:

 private Material material;
 
 
 void Start ()
 {
 material = new Material ("shader/shader");
 }

Making sure that the only reference to this material is that material variable, if I were to null it:

 void Update ()
 {
 material = null;
 }

Does it also destroy the material? I ask this because I know that Unity wraps c++ objects in c#. So null would make the c# object get garbage collected, but does the c++ side get destroyed aswel? Or do you have to use Destroy () to ensure it does?

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

Answer by fafase · Jun 11, 2015 at 06:14 PM

I would use Destroy. I guess it runs and removes all possible references to the object, so it is sure to get collected.

Also, a null may not work the same in Unity, consider the following:

 GameObject obj = new GameObject();
 Rigidbody rig = obj.AddComponent<Rigidbody>();
 rig.AddForce(force);
 rig = null;
 obj = null;

the script in which this happens has no access to the object anymore nor the rigidbody, you may think they will be collected. But the GO is still in the scene with a rigidbody attached to it.

So, for a null to work, you would have to gather all containers and references and make the separations from there.

Conclusion: Use Destroy.

Comment
Add comment · Show 3 · 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 Dave-Carlile · Jun 11, 2015 at 08:05 PM 0
Share

I assume the GameObject appears in the hierarchy when you create it like this? If so, there's still a reference to it so nulling your local variables are just eli$$anonymous$$ating your reference. Just have to remember that GameObjects are kind of special in that the engine creates its own reference when you construct one. You have to remove the engine's reference if you want it to go away, and that is done with Destroy.

avatar image Bonfire-Boy · Jun 11, 2015 at 08:50 PM 0
Share

This all seems right, except that, as I understand it, it's not just GameObjects which aren't garbage collected, Unity's Object class isn't either.

It's relevant because the $$anonymous$$aterial in the question is an Object but not a GameObject.

avatar image fafase · Jun 12, 2015 at 07:44 AM 0
Share

The main issue with the question is that we do not know exactly what happens when we create a new Object or a new GameObject. $$anonymous$$ost likely, we do not have access to every reference the engine is creating. So using Destroy is the solution.

@Dave Carlile, yes the GameObject is in the hierarchy and well alive as it can be fetched using Find or some Physics related method.

$$anonymous$$y point is mainly that if using a UnityEngine item, trying the conventional C# way is not reliable.

But I guess we already all agree with that.

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

how to destroy enemy 1 Answer

Why are objects just set to null? 2 Answers

Multiple Cars not working 1 Answer

Enemy Spawner help 1 Answer

Can you destroy an object using GetInstanceID? 0 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