- Home /
How to fully instantiate a Prefab from within a script, NOT dragging a reference to the object in the scene?
I'm finding it hard to believe you can't instantiate a prefab game object from code without dragging the prefab into an already existing object to give it the reference. However, that's the only instructions I'm seeing online.
Example from https://docs.unity3d.com/Manual/InstantiatingPrefabs.html
// Reference to the Prefab. Drag a Prefab into this field in the Inspector.
public GameObject myPrefab;
I don't want to drag anything. In code only, I simply want to create a game object and then set it to the prefab, or create the prefab which inherently makes the game object. Something like this:
GameObject player;= new Prefab("Player1.prefab");
player.name = "Player 1";
player.transform.position = new Vector3(0, 0, 0);
Does this functionality exist? If not, I'm left with creating my game object line by line in my code.
Answer by VStopka0 · May 24, 2020 at 07:32 PM
Put the prefab to Resources, then use Resources.Load
Thanks. Where can I find documentation on that? Or do you need to understand Resources.Load itself to come to that conclusion?
Not that I'm saying it isn't my fault, but I would have had no clue that this is the way you instantiate prefabs just by that landing page.
Your answer
Follow this Question
Related Questions
Prefab component not linking to destination 1 Answer
Change script from EditorWindow 0 Answers
How to run script in editor 1 Answer
Mech building game Parsing a large number of Prefabs 0 Answers
Script Not Working On Prefab 2 Answers