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 FrancisDuranceau · Feb 02, 2012 at 03:32 PM · instantiateprefabdestroymemory leak

Prefab instantiation and destruction

Hello,

I'm pretty new to Unity and even though I started to read the doc and some books, I still have questions I don't find an answer for.

So, let say I want to throw rockets on a wall when I hit a key, I would write a script to instantiate prefab and throw the prefab on my wall.

My question is : is there anything special that occurs when my rocket that miss the wall goes out of the scene or do I still have to find a way to destroy it otherwise it will leak?

Also, maybe I should have find this in the documentation so if someone can point it out to me, I'll gladly read it.

Thank you

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

3 Replies

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

Answer by Berenger · Feb 02, 2012 at 03:49 PM

If you don't do anything about it, it's going to exist as long as your scene does. If there is one or two, that's not a huge problem, if you have a couple thousand, you're in trouble ! So, a common trick to do that, create an empty game object in the middle of your scene with a box collider so big it contain everything, set to trigger. Add a script that destroy everything that comes out of it (OnTriggerExit), and pray for those poor souls. That should do it :)

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 BiG · Feb 02, 2012 at 03:51 PM 0
Share
  • for the poor souls! ;)

avatar image
1

Answer by BiG · Feb 02, 2012 at 03:48 PM

Yes, objects continue to remain in memory until you won't call function Destroy() on them.

As an example, I've recently made an arcade shoot 'em up, so I attached this script to my projectiles:

 function Start(){
    yield WaitForSeconds(5);
    Destroy(gameObject);
 }

With that, my projectiles free the memory 5 seconds after their creation. In fact, after such a time, they already hit an enemy, or they are already out from the field of view. I don't want to have thousands on them occupying memory and CPU!

When this implementation isn't possible (or nice), you can have invisible walls, surrounding the perimeter of the scene, and make some objects destroy themselves whenever they hit these walls.

Is not clear, from your question, if this was your only doubt, or you are also asking about instantiating a prefab when pressing a key (I don't know if that was a premise for what I've just said, or a question itself).

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 Berenger · Feb 02, 2012 at 03:52 PM 0
Share

That's an other way to do it, but you don't really need the yield. The second parameter of Destroy is doing exactly that. I'm not a big fan of that solution usually, if you have slower projectiles for examples, the destruction might occure in the player's sight.

avatar image BiG · Feb 02, 2012 at 03:57 PM 0
Share

You're totally right, I could write Destroy(gameObject, 5);

Concerning the "slower projectiles", yes, it is: you have to test the behaviour of all kinds of shoot, and make sure that they don't disappear when they are in the FOV. However, I specified the use of this technique in a 2D arcade game - you would admit that's an easy workaround in such a context.

avatar image
0

Answer by FrancisDuranceau · Feb 03, 2012 at 01:24 AM

Thank you for all of your answers.

@BiG I'm fine with the instantiation of the rockets.

Those solutions are good for me, either use a yield, the second param of destroy or an invisible wall that will destroy on collisions.

It's not really an advanced topic but when I'm reading all the examples on how to instantiate a rocket, they never destroy the instances and they fall out somewhere in the void under my scene. I was simply worried about memory.

Francis

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 BiG · Feb 03, 2012 at 10:04 AM 0
Share

You're right Francis. Often, memory leak problem is underestimated in tutorials, because the focus is put on instantiation, translation, and other operations that are "essentials" for the behaviour of the game. $$anonymous$$emory leak problem is also negligible in a short example, when you instantiate 1-2 objects, that's the reason why that's not always contemplated in examples!

  • to your question, having highlighted this issue.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to destroy camera instatiated from prefab? 0 Answers

Can't remove instantiated prefab 0 Answers

Instantiated gameObject(here, wall) destroyed, but when checked for existence, shows that it exists, how? 1 Answer

Instantiated object is destroyed in scene, but still logs as existing in console. 1 Answer

Removing A Component From An Instantiated Prefab After X More Are Instantiated 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