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
4
Question by Essential · May 15, 2013 at 07:57 PM · instantiatesceneperformance

Performance difference between having objects in scene upon load vs instantiating after load

Is there any difference in having objects stored in a scene versus instantiating them in Awake of the scene, or are both approaches doing effectively the same thing behind the scenes?

Comment
Add comment · Show 11
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 DannyB · May 15, 2013 at 08:08 PM 1
Share

Was wondering exactly the same recently. Good question. But somehow, I have a feeling it will be the same, give or take a few ticks.

avatar image Julien-Lynge · May 15, 2013 at 08:11 PM 0
Share

I would actually guess not. We have very complex scenes, and instantiating (say) a thousand objects from script can cause a quite noticeable hiccup. In fact, just changing the transform hierarchy at runtime (e.g. by changing parents) is very slow. Either way, curious to hear the answer.

avatar image code-blep · May 15, 2013 at 08:22 PM 0
Share

I'm with DannyB. That is the aim of pooling objects isn't it? Still curious though ;) Also is there a reason you ask?

avatar image DannyB · May 16, 2013 at 06:50 PM 1
Share

@Essential, there once was a guy at Unity who knew the inner workings, unfortunately, he left...

avatar image kromenak · May 16, 2013 at 07:35 PM 1
Share

I'd guess that one benefit to having everything in the scene as opposed to instantiating after load is that you can do an async load if stuff is already in the scene - which should avoid the game appearing to freeze while loading in assets and such. For example, we had a game where we performed LoadLevelAsync, but it would freeze up anyway because we instantiated a bunch of stuff after loading. I don't believe there is a way to asynchronously instantiate stuff?

Another $$anonymous$$or thing is that the placement of things in a scene is saved in the scene file. If you are placing A LOT of stuff, you could see your scene file size increase - and particularly on mobile this can be a bad thing. If you ins$$anonymous$$d instantiate at runtime, the scene file size can stay relatively small.

Finally, I've found instantiating after loading is good for stuff that you know must be a certain way all the time, and you don't want to worry about inconsistencies between levels - i.e. if every level must have a camera, I might just instantiate my camera prefab on level load ins$$anonymous$$d of having to place a camera manually per level and risk missing one.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by rutter · May 16, 2013 at 07:52 PM

In my experience, Instantiate() is never faster than pre-loading objects, but the difference is negligible in typical use. Unless you're loading in hundreds or thousands of objects, I wouldn't worry about it.

Instantiate() causes a very small performance hit, in both time and memory; it's only going to matter if you're doing a lot of it. If you're on mobile, it'll matter a bit sooner, but it's about the same deal.

If you're worried about optimization:

  • Find ways to pre-load more objects in the scene.

  • Find ways to "pool" objects, so that they can be recycled. Instead of destroying them when you're done, make them inactive and reset them when you need them again.

(When I make object pools, the important ones are still backed by Instantiate() in the event that I run out of objects.)

Sometimes, it's the best tool for the job. Like kromenak mentioned in comments, it's handy to load in prefabs that you'd like to see consistently in every scene: cameras, game managers, and the like.

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 Julien-Lynge · May 16, 2013 at 07:55 PM 1
Share

I'd add to that:

Beyond any small hit for Instantiate, changing transform hierarchy in my experience incurs quite a bit of penalty for large numbers of objects, and there's no way I know to Instantiate as a child of another object. So if you have any complex kind of structure, you have to account for that. There's no changing parents when loading a scene.

avatar image Essential · May 16, 2013 at 08:39 PM 0
Share

Good advice @rutter.

@Julien.Lynge You can instantiate as the child of another object by doing:

 newObject = Instantiate( PrefabObject );
 newObject.transform.parent = myParentObject;
avatar image Julien-Lynge · May 16, 2013 at 08:42 PM 2
Share

Right, that's what I'm saying - that extra 'parent = X' step after the instantiation adds time to the process, and not an insignificant amount if you're instantiating hundreds or thousands of objects (which we do).

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

Instantiating prefab vs loading new scene 1 Answer

Instantiate vs caching GameObject in scene 3 Answers

Are the prefabs stored in RAM or in the Hard disk? 0 Answers

Unity performance issues. 0 Answers

Where to position idle gameobjects referenced in Instantiate 2 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