user3792283
user3792283

Reputation: 25

How to instantiate nonpublic prefab

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

Answers (1)

axwcode
axwcode

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

Related Questions