how to instantiate prefabs at runtime?

I’m having trouble instantiating a prefab at runtime. I tried to use this code to do instantiation.

GameObject spherePrefab;
spherePrefab = GameObject.Instantiate(Resources.Load(“spherePrefab”));
print(Instantiate(Resources.Load(“spherePrefab”)));

and got this error

Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?)

how can I instantiate prefabs on the fly??, thanks for any help in advance! :slight_smile:

and this line

spherePrefab = GameObject.Instantiate(Resources.Load(“spherePrefab”));

in the " ", there is no space in between spherePrefab, I don’t know why it is spacing but I could not edit it :frowning:

can you just set it as a variable then instantiate it?

No, because this character will be instantiated at run time and then it will instantiate spherePrefab during the game. I could only set this in code not in inspector. :frowning:

You’re missing an as:

spherePrefab = GameObject.Instantiate(Resources.Load(“spherePrefab”)) as GameObject;

how silly of me to forget this :slight_smile:
thanks a lot Izitmee, I’ve got it worked :slight_smile:

:slight_smile: