• 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 Architekt · Sep 26, 2014 at 11:20 PM · instantiate prefab

Instantiate prefab differences...

Let's say I instantiate a prefab like this:

 GameObject obj = Instantiate(Resources.Load("MyPrefab"), pos, rotation) as GameObject;

Is there a difference at all from if I had a public GameObject variable in my class, and then in the editor dragged a "MyPrefab" prefab to it, and then in code did:

 public GameObject objToInstantiate; // set to "MyPrefab" object in editor
 
 ...
 GameObject obj = Instantiate(objToInstantiate, pos, rotation) as GameObject;

Thanks!

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

Answer by Baste · Sep 27, 2014 at 08:03 AM

In efficiency - how much time it takes to make the thing - there shouldn't be too big a difference between the two methods.

The big downside with Resources.Load is that anything in the resources folder is included in your build. When you build your game, Unity grabs every object that's referenced in a scene, and only those objects. This means that say a material that's in your materials folder, but is never used, will not be included in your final game file (.exe or .app or whatever).

Stuff in the resources folder are the exception to this. As you can load those from script by giving the string path to the resource, Unity has no idea what parts of the Resources folder it should include or not, so it puts the entire folder in your build. So if you stop using an asset that you used to load from Resources, but forget to remove it from the folder, it will still be included in the final build.

The general advice is to avoid using Resources.Load. There's also some comments on the same thing in the official docs, I'd suggest you take a look at what Unity says there.

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 fafase · Sep 27, 2014 at 08:44 AM 0
Share

Other downside of the Resources folder is you are tight to the path where you object is stored. If you change the path of the name you also need to change it in code. There are still possibility to load all object of type from an entire folder but all in all drag and drop makes it easy.

One convention we used is to make the reference private/protected and use the SerializeField attribute. This way you get the best out of Unity without breaking OOP principles.

avatar image buskamuza · Nov 26, 2015 at 02:55 AM 0
Share

How about memory usage in both cases?

Setting a prefab via Editor is more convenient than loading it from resources.

But what, if I need a prefab that may be used or may be not. And this prefab has a dependency on another prefab that possibly won't be used. And so on. Let's say I have a chain of 10 dependent prefabs: objA -> prefabB -> prefabC -> prefabD -> prefabE -> prefabF -> prefabG -> prefabH -> prefabI -> prefabJ. In case none of them will be used, the memory will be still allocated for these objects. But how much? What is prefab before it's instantiated? Is it the same as instantiated object and then I'll get whole chain of 10 prefab objects? Or is it something serialized and it does not load other objects it references to until it's instantiated?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

unity gameObject set id before instantiation 3 Answers

Having problem with Instatiate GameObject at Canvas 0 Answers

Im trying to instantiate prefab from an array but get a confusing response...(this prefab will be consumed by the player meaning it needs to be instantiated multiple times) 2 Answers

Prefab attached to script is destroying itself before Instantiate() is called 1 Answer

Spawn a prefab off screen? 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