• 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
5
Question by Databases · Apr 23, 2016 at 11:56 AM · unity 5gameobjectinstantiateresourcesresource.load

Instantiate(GameObject) VS Instantiate(Resource.Load(Object Path))

Hi everyone,

So, my question is straight :)

When i can instantiate a gameobject like this >> Instantiate(GameObject) << and do what ever i would like with it, then what is the benefit of instantiating a gameobject from inside of the Resources folder like this >> Instantiate(Resource.Load("object path")) << since the result is just exactly the same!!!!??

Thank you for your time.

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

Answer by Soraphis · Apr 23, 2016 at 01:07 PM

You don't need to attach the Prefabs to an Component.

To use the Method Instantiate(GameObject) you would write a new component, create a new variable of type GameObject, attach the component to an GameObject, and fill the variable in the inspector.

To use the Method Instantiate(Resource.Load("object path")) you just need the name/path of the Prefab.

this is extremely useful if you have a huge amount of generated parts in your game (so there are no gameobjects placed in the editor), if you'd want to avoid Resource.Load you'd need some "data-holder-gameobject" placed in an nearly empty scene. edited to make my point a bit clearer

it is aswell helpfull if you have large number of different Prefabs and your method knows the name of the object it wants to build, or you just simply dont want to drag and drop all those prefabs into the inspector window


edit 2: since you seem interested in this, here are some additional thoughts:

  • Resource.Load, loads data from your drive. it's possible that your game is played from a Hard drive, which would mean to load the prefabs the hard drive needs to rotate, position the read-head, and so on.

  • Instantiate is slow itself even without the need of Resource.Load Instantiate is not that fast. if it happens that you need it very often ( multiple times per second) you should consider some kind of object-pool 1


edit 1:

well, i'll provide a simple example: let's say you are doing a 3d-Pokemon-Generation1-game. so you'd have a folder "resources/pokemon" in this folder there are prefabs to all you pokemon-avatars (the 3d representation of the pokemon, with scripts to move, an ai to hide in grass, the animated model and so on) well in the first pokemon generation there are 151 pokemon, so you'd have 151 assets you'd need to attach to a component in the inspector.

or you could write a component which will to the spawning for you like this:

     private Dictionary<string, GameObject> Pokemon = new Dictionary<string, GameObject>();
 
     public void SpawnPokemon(string name) {
         if (!Pokemon.ContainsKey(name)) {
             var go = Resources.Load<GameObject>("pokemon/" + name);
             if (go == null) {
                 Debug.Log("tried to instantate pokemon " + name + ". but it does not exist");
                 return;
             }
             Pokemon.Add(name, go);
         }
         GameObject.Instantiate(Pokemon[name]);
     }

we load the pokemon-prefabs as we need them via Resources.Load and store them in a dictionary, so we need to lookup the file just one time (per game session).


Comment
Add comment · Show 9 · 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 Databases · Apr 23, 2016 at 01:45 PM 0
Share

Thanks a lot Soraphis,

So what you have just explained makes sense in terms of when using a huge amount of gameobjects(parts) and using Resouce.Load would be better for extreme ease of use so i don't have to create variables and then assign gameobjects in the inspector one by one, but what about performance is there any noticeable difference in the performance? Wouldn't >> Instantiate(Resource.Load("object path")) << be costly because it will try to look for the gameobject while >> Instantiate(GameObject) << will just directly use the already assigned gameobject?I know there are a bunch of questions regarding performance but from your experience i would like to know your perspective.

Regarding the gameobjects-holder, this can be done using >> Instantiate(GameObject) << as well!

Thanks.

avatar image Soraphis Databases · Apr 23, 2016 at 02:14 PM 0
Share

it's exactly as you said, Instantiate(Resource.Load("object path")) will search for a file with this name in all "resource" folders of your project and you should consider some kind of caching (saving the prefab in a variable of your script) if you use the same prefab frequently (e.g. each frame)

Regarding the gameobjects-holder, this can be done using >> Instantiate(GameObject) << as well!

edit: did not read carefully enough, understand this totally wrong :P

which will leave you with an additional prefab, which holds references of your prefab and needs to be instantiated and therefore either referenced from an game object in your scene (lets call it gameobjects-holder ;D). or via its path (which would be against the purpose of using a gameobjects-holder in the first place)

avatar image Databases Soraphis · Apr 23, 2016 at 02:58 PM 0
Share

which will leave me with a gameobject (not additional prefab) that references the prefab which i'm willing to instantiate, so where is the additional prefab? I'm not trying to say you are wrong, i may just didn't understand you well so that's why i'm replying i want to get to understand things correctly.

So i see that the difference is that i will just have to reference the prefab from a gameobject in the scene(say the game manger) BUT not loading it from the resource folder directly, so how this can be against the purpose of using gameobjects-holder? I'm sorry but i got some confusion here.

Thanks.

Show more comments
avatar image Databases · Apr 23, 2016 at 03:53 PM 0
Share

Thanks a lot i do really appreciate your prompt help!

avatar image Ziplock9000 · Feb 22, 2019 at 02:17 AM 0
Share

What about memory usage BEFORE the instantiation. Obviously the Resource.Load version will use nothing, but the GameObject version, is the prefab already resident in memory that it makes the instance from?

avatar image Soraphis Ziplock9000 · Feb 22, 2019 at 10:08 PM 0
Share

Obviously the Resource.Load version will use nothing

well, it will use up between 10kB and 100kB depending on the size of your prefab. but on your HDD/SSD. When you use Resource.Load you'll load this memory into your RA$$anonymous$$ to use it.

If you store a reference to the GameObject unity will load it into the RA$$anonymous$$ at GameStart / SceneStart.

if the game is on an HDD loading (even small) assets can generate hickups and lags, ideally you wanna load everything you need at Start so that you're not depending on access times of HDDs.


RA$$anonymous$$ usage before the instantiation is only interesting if you have a device with very limited RA$$anonymous$$ and only if you need it all. while i'm writing this, my browser has 2.5GB RA$$anonymous$$ allocated. and its just a browser. a prefab is a text file, even if it gets as large as 1$$anonymous$$B (10 times as large as prefabs usually get) -> they are not worth to optimize RA$$anonymous$$ usage.

also: in a built those sizes should be smaller, those are Edit-Time Values.

caring about (asset) RA$$anonymous$$ usage is in most cases unnecessary(, there might be exceptions).

avatar image Ziplock9000 Soraphis · Feb 22, 2019 at 11:58 PM 0
Share

What about referenced objects that are not enabled. I've read they take up RA$$anonymous$$ on scene load for meshes/textures/scripts but not pushed to VRA$$anonymous$$ until they are enabled?

Show more comments

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

79 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 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 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 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

How do i Instantiate gameObjects in between multiple points? 2 Answers

How do i Instantiate Objects to a Empty GameObject as a Child?? 3 Answers

[Solved] How to instantiate GameObject with different rigidbody parameters 1 Answer

Creating a GameObject variable without instantiating it? 1 Answer

How does resource loading and instantiating consume memory 0 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