Reputation: 25
I instantiate prefabs by dragging them into a variable in the scene. Here's the code:
public GameObject player1;
void Start()
{
Instantiate(player1, spawn.position, spawn.rotation)
}
I don't want to drag and drop. How do I accomplish this?
Upvotes: 1
Views: 979
Reputation: 7824
Put your prefab in the Assets/Resources
folder.
Then you can find the prefab and then instantiate it.
private GameObject player1;
void Start()
{
player1 = Instantiate(Resources.Load("Player1"), spawn.position, spawn.rotation) as GameObject;
}
"Player1"
is the name of the prefab. You can call it what you want.
You can do this with any prefab or other data, such as textures. More information on Resources.Load
on Unity API.
Upvotes: 2