Question by
Mr_Clement_FR · Aug 04, 2020 at 03:43 PM ·
gameobjectinstantiateprefabsprefab-instance
Instantiate more than 1 prefab
Hello guys!
I'm really new to Unity and coding in general, so forgive my noob question: in the following code, I made a gameobject spawn one type of prefab (macarons) and it works as intended.
My question is: How do I make it spawn other prefabs as well, randomly? I can't seem to be able to add a second line to add another prefab in the Inspector.
using System.Collections;
using UnityEngine;
public class MacaronSpawner : MonoBehaviour
{
private GameObject instantiatedMacarons;
public GameObject macaronPrefab;
public Transform[] spawnPoints;
public float mindDelay = .1f;
public float maxDelay = 1f;
// Start is called before the first frame update
void Start()
{
StartCoroutine(SpawnMacarons());
}
IEnumerator SpawnMacarons ()
{
while (true)
{
//Spawn des macarons
float delay = Random.Range(mindDelay, maxDelay);
yield return new WaitForSeconds(delay);
int spawnIndex = Random.Range(0, spawnPoints.Length);
Transform spawnPoint = spawnPoints[spawnIndex];
GameObject spawnedMacaron = Instantiate(macaronPrefab, spawnPoint.position, spawnPoint.rotation);
Destroy(spawnedMacaron, 4f);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
The prefabs stop instantiating after some time. 1 Answer
ExecuteInEditMode is not working when trying to instantiate a new GameObject [SOLVED] 1 Answer
Any other ways for instantiated prefabs to reference game-objects in the scene? 0 Answers
Prefabs not turning blue and losing connection 6 Answers
Create gameobject in other scene... 0 Answers