user10865688
user10865688

Reputation:

Can't instantiate prefab inside prefab

Evening guys, I have a question of why I can't instantiate prefab inside prefab?

enter image description here

enter image description here

ObsSpawner Script:

public GameObject[] Spawner;

// Start is called before the first frame update
void Start()
{
    Spawner = GameObject.FindGameObjectsWithTag("SpawnObj");
}

// Update is called once per frame
void Update()
{
    foreach (GameObject spawn in Spawner)
    {
        spawn.SetActive(true);
    }
}

but if I put the spawner caller outside prefab that will instantiate those obstacles, is it anything wrong with my script?

Upvotes: 1

Views: 96

Answers (1)

Ruzihm
Ruzihm

Reputation: 20269

You already have references to the prefabs. Just iterate through and Instantiate them:

public GameObject[] Spawner;

// Start is called before the first frame update
void Start()
{
    foreach (GameObject spawn in Spawner)
    {
        Instantiate(spawn);
    }
}

Upvotes: 1

Related Questions