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 cdrandin · Aug 07, 2013 at 02:35 AM · instantiateperformancedestroy

Instantiate vs caching GameObject in scene

Was just wondering when would be a worthy time to even use Instantiate?

I understand how it works and its convenient to bring about GameObjects into the scene like Prefabs, but couldn't one simply just either Instantiate it once at run time or simply have the object in your scene before it is ran.

Same with Destroy. They are said to be expensive with repetitive calls, so I figure just move your GameObject far away from the scene as possible and deactivate the component and only re-position and reactivate when needed.

Is there a "good" reason to prefer Instantiate/Destroy over the methods I have provided and possibly others?

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 Adamcbrz · Aug 07, 2013 at 03:56 AM

Yes there is! You are correct if you know you are going to use something then by all means preload it. But sometime you need the extra overhead to not preload everything. You can also gain some memory back by unloading the object. Now for an example.

Say you have a game like a MMO with an inventory system for your weapons. I wouldn't want to instantiate/preload every weapon you could possible use at the load of the level. You would only load the weapon the player is currently carrying.

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 cdrandin · Aug 07, 2013 at 04:17 AM 0
Share

ah good point, but how about the case of an open world environment? I guess you could put item spawn points at given locations and if a player is near by, instantiate the desire object, assu$$anonymous$$g the spawn point has a reference to the desired object, but if there are many items this feels like it could get messy quick. Any thoughts?

avatar image Adamcbrz · Aug 07, 2013 at 04:24 AM 0
Share

Well it depends. If it's a problem you could do a Borderlands style and have treasure boxes and you don't know what you get until you open it. At that point it's a design decision.

avatar image cdrandin · Aug 07, 2013 at 04:31 AM 0
Share

Alrighty, thanks for the insight.

avatar image
2

Answer by jannamcl · Aug 07, 2013 at 04:53 AM

Using instantiate/destroy reduces update calls on certain objects that you are not currently using. Say you have a gun that needs to shoot 100 bullets in your scene - you would not want all of those bullets to be constantly updating when you are not even shooting your gun. Besides, do you really want to have all those bullet prefab copies crowding up your Hierarchy in the editor?

Some objects may want to log how many frames they were active for or their change in time. Your objects start() method may want to get the current time/frame and begin from that time. If you create the object at the beginning of your scene, the start() method for it's component may run unless you are sure the component script was properly disabled before it was called.

You may also want some objects that have the same tag active at different times. For example, you have Objects A, B and C that share Tag "MyObject". You may want Objects A and B to be active, while Object C is disabled which you only moved out of the view for your scene. Ideally, you might want to call findObjectsByTag("MyObject") and get/set some variables in your attached component scripts. You would then have to loop through each object to check if it's component script is enabled or disabled, which may be very slow depending on the number of objects you found. You do not need to handle this if Object C does not exist because it was never instantiated or was already destroyed.

So to sum up, I would use instantiate/destroy if you don't want to constantly update or find those objects, which is useful when trying to speed up overall performance.

Additional tip: If you want to initialize objects/scripts before your game ever starts, you can use the Awake() function, which runs before the Start() function. This is described more in the following links from the scripting reference: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Start.html http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Awake.html

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
avatar image
0

Answer by Brian-Brookwell · Nov 10, 2014 at 10:51 PM

This is a somewhat related problem. I've several thousand trees and bushes that need placed (procedurally, of course) when a game starts. Currently, timing checks indicate that 13 seconds of the 16 seconds that it takes to build a simple level are in the section that's Instantiating the tree/bush/mushroom game objects. Is there any way to create the pool in the editor mode so it's already available when game starts. Unless I misread Awake, it's still going to run at the time I click on my game to start. The pre-allocation isn't going to help in this case.

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

17 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

Related Questions

Instantiate/Destroy Garbage Collection advice 1 Answer

Replace GameObject vs. replacing mesh and material? 2 Answers

Objects will not destroy after they have been instantiated 1 Answer

How to destroy instantiated object 1 Answer

How to destroy an instantiated prefab object and keep instantiating it 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