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 AlfAtUnity · Jul 04, 2013 at 12:42 PM · gameobjectdestroy

Need to manually destroy GameObject?

Should I destroy GameObject manually when it goes out of scope? (Since GameObject class must be a wrapper around native class, the answer - yes. But i'm not sure.) In this code GameObject is locally scoped.

 void foo()
 {
  GameObject mGameObject = new GameObject("name");
  // Destroy(mGameObject); => WE WON'T CALL THIS METHOD
 }

We didn't call Destroy(mGameObject); => it will persist in memory (memory leak?) Should we always manually destroy locally scoped GameObjects (and similar objects then have Destroy() method) when they go out of scope?

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
1
Best Answer

Answer by Em3rgency · Jul 04, 2013 at 12:53 PM

No, you don't need to do this. And in this example (I miss remember the exact message) but it would tell you that you're trying to access something out of scope as an error.

EDIT: Ok, so for one thing, I doubt you can even "new" a game object. Shouldn't all game objects be instantiated? And if so, a game object will remain instantiated (and use up resources) until you manually destroy it. Here is an older question on a similar subject http://answers.unity3d.com/questions/209101/do-un-active-game-objects-still-use-memory.html

Now, if we're talking other classes that you allocate memory for with new, as far as I've checked, there is no "delete" to accompany the new like in c++, so I assume freeing up memory is done automatically. At least I sure as hell hope so.

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 AlfAtUnity · Jul 04, 2013 at 01:29 PM 0
Share

Oh,I actually didn't mean this (it's my fault). By "Destroy(mGameObject);" I mean: was object already destroyed OR it wasn't destroyed? (actually became memory leak that will be persistent untill we unload the whole scene).

avatar image Em3rgency · Jul 04, 2013 at 01:40 PM 0
Share

Edited my answer accordingly :)

avatar image AlfAtUnity · Jul 04, 2013 at 02:33 PM 0
Share

Actually, I'm not a programmer (I'm graphic designer who just learns game program$$anonymous$$g) but I found this: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GameObject.html

Look, the link you provided is about changing particular fields of objects. It's affect the following loop in Unity (must be implemented something like this):

 //pseudocode
 void update()
 {
    foreach(GameObject in list of Entities)
         if(GameObject.active == true)
             GameObject.update()
  
 }

You're right w/ last statement: "normal" C# objects will be destroyed when go out of scope. But, you're right again, we talk about objects that wrap native classes. So, the destruction is trickier here and can depend on implementation done by Unity. I think your answer is complete enough to be checked as accepted!

avatar image Em3rgency · Jul 04, 2013 at 02:35 PM 0
Share

Well the link is a discussion about memory conservation. Either by disabling the object or destroying it. As both cases were exa$$anonymous$$ed, I thought it was on subject :)

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

16 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Why does my bullet prefab get destroyed? 1 Answer

Destroying Game Object On MouseButtonDown + Distance Collision 0 Answers

How to Play 3D Sound and Keep Script on Multiple Prefabs after Destroying a gameObject? 0 Answers

References to GameObject become null 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