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 Mystic_nynja · Mar 24, 2015 at 06:06 AM · instantiateprefabruntimedynamic

Instantiate a prefab vs create one dynamically at runtime?

I am wondering if there is a performance difference between loading a prefab into the game vs creating a gameobject programatically. I guess I can do either way, but was wondering if one is faster than the other, or if one takes less memory.

Let's say I have 200 objects as prefabs and I only want to select 5 of them, vs. figuring out which 5 I want and then just creating them through code. Is there a huge memory difference between having those 195 prefabs not being used?

Like I said I can do it either way, and I don't think there would be a performance hit but just wanted to see if the community knew any different.

The only thing I could think of is that if the prefabs are big in size that would be the only disadvantage, but I think they would be small so we can rule that out as a factor.

Any insight would be great!

Thanks, Will

Comment
Add comment · Show 2
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 AlwaysSunny · Mar 24, 2015 at 05:44 AM 0
Share

There's a small performance cost associated with instantiation, but this is often hidden behind a load screen, or otherwise parceled out over time to hide the cost. Also look into object pooling if you need the same prefabs many times (e.g. projectiles).

It's not usually a question of performance, but of sense. It is either more convenient and sensible to pre-place objects in the scene or to create them on the fly. Do what makes the most sense.

WRT having hundreds of unused objects, it would be wise to avoid that situation if possible, which leads me to assume instantiating would be better in your scenario.

avatar image Mystic_nynja · Mar 24, 2015 at 12:50 PM 0
Share

That helps. Thanks :) After some thinking last night I think I have to go with dynamically creating them because of how different the object can be.

Thanks again!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ericool · Mar 24, 2015 at 04:26 PM

when you are playing with the editor, it loads your public prefabs or scripts so there is no worry with that , but a good practice is to load them , even other resources and when needed you copy them . That way you wont have any surprise if the prefab was too huge. For the build , big prefabs should not be at runtime, my opinion , most of the time the prefab are always the same and are always instantiate at runtime .Just reminder that for a small game it is ok , but for a huge one , you might want to keep that memory for something else instead .

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Prefabs Transforms LookAt 0 Answers

Instantiating scripts with references to prefabs loses the reference. 1 Answer

Instantiated prefab click script running on all instances 6 Answers

Changing game object based on variable state? 1 Answer

Avoid altering prefab at runtime 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