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

Hi everyone,

So, my question is straight :slight_smile:

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.

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).