- Home /
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.
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).
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.
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)
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.
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?
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.Obviously the Resource.Load version will use nothing
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).
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?
Your answer
Follow this Question
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