- Home /
How can I make Random Obstacle Spawn randomize between 4 or more prefabs?
Hello I am new to Unity and I've been trying to make a vertical endless runner in which I'm trying to randomize obstacles, I have already set up for one prefab, but I'm trying to make it randomly choose between 4 prefabs. Here's what I have so far.
enter code here public Transform prefab;
public int numberOfObjects;
public float recycleOffset;
public Vector3 startPosition;
public Vector3 minSize, maxSize, minGap, maxGap;
public float minY, maxY;
private Vector3 nextPosition;
private Queue<Transform> objectQueue;
void Start () {
objectQueue = new Queue<Transform>(numberOfObjects);
for(int i = 0; i < numberOfObjects; i++){
objectQueue.Enqueue((Transform)Instantiate(prefab));
}
nextPosition = startPosition;
for(int i = 0; i < numberOfObjects; i++){
Recycle();
}
}
void Update () {
if(objectQueue.Peek().localPosition.y + recycleOffset < Player.distanceTraveled){
Recycle();
}
}
private void Recycle () {
Vector3 scale = new Vector3(
Random.Range(minSize.x, maxSize.x),
Random.Range(minSize.y, maxSize.y),
Random.Range(minSize.z, maxSize.z));
Vector3 position = nextPosition;
position.x += scale.x * 0.5f;
position.y += scale.y * 0.5f;
Transform o = objectQueue.Dequeue();
o.localScale = scale;
o.localPosition = position;
objectQueue.Enqueue(o);
nextPosition += new Vector3(
Random.Range(minGap.x, maxGap.x),
Random.Range(minGap.y, maxGap.y)+ scale.y,
Random.Range(minGap.z, maxGap.z));
if(nextPosition.y < minY){
nextPosition.y = minY + maxGap.y;
}
else if(nextPosition.y > maxY){
nextPosition.y = maxY - maxGap.y;
}
}
}
Answer by Foose · Aug 15, 2014 at 11:55 AM
I always use this one. It lets you handle how much prefabs you wanna take, choose a random prefab, vary the spawntime and takes gameobjects as spawnpoints.
public class mobGenerator : MonoBehaviour {
public enum State {
Idle,
Initialize,
Setup,
SpawnMob
}
public GameObject[] mobPrefabs; //an array to hold all of the prefabs of mobs we want to spawn
public GameObject[] spawnPoints; // this array will hold a reference to akk the spawn points in the scene
public State state; //this is our local variable that holds our current state
public int mobsNumber = 1;
public float countSpawnTimer;
public float spawnTimer = 10;
GameObject child;
void Awake () {
state = mobGenerator.State.Initialize;
countSpawnTimer = spawnTimer;
}
// Use this for initialization
IEnumerator Start () {
while (true) {
switch(state) {
case State.Initialize:
Initialize();
break;
case State.Setup:
Setup();
break;
case State.SpawnMob:
SpawnMob();
break;
}
yield return 0;
}
}
void Update() {
countSpawnTimer -= Time.deltaTime;
if (countSpawnTimer <= 0) {
countSpawnTimer = 0;
state = mobGenerator.State.Initialize;
}
}
private void Initialize() {
countSpawnTimer = spawnTimer;
if (!CheckForMobPrefabs ())
return;
if (!CheckForSpawnPoints ())
return;
state = mobGenerator.State.Setup;
}
private void Setup() {
state = mobGenerator.State.SpawnMob;
}
private void SpawnMob() {
GameObject[] gos = AvailableSpawnPoints ();
for (int cnt = 0; cnt < mobsNumber; cnt++) {
GameObject go = Instantiate(mobPrefabs[Random.Range(0, mobPrefabs.Length)],
gos[Random.Range (0, gos.Length)].transform.position,
Quaternion.identity
) as GameObject;
go.transform.parent = gos[cnt].transform;
Destroy(go, spawnTimer);
}
state = mobGenerator.State.Idle;
}
//check to see that we have at least one mob prefab to spawn
private bool CheckForMobPrefabs() {
if (mobPrefabs.Length > 0)
return true;
else
return false;
}
//check to see that we have at least one spawnpoint to spawn mobs
private bool CheckForSpawnPoints() {
if (spawnPoints.Length > 0)
return true;
else
return false;
}
//generate a list of available spawn points that do not have any mobs child to it
private GameObject[] AvailableSpawnPoints() {
List<GameObject> gos = new List<GameObject> ();
for(int cnt = 0; cnt < spawnPoints.Length; cnt++) {
if(spawnPoints[cnt].transform.childCount == 0) {
gos.Add(spawnPoints[cnt]);
}
}
return gos.ToArray ();
}
}
Awesome. I just implemented the array you have onto my code and its working pretty well Thanks
I am having a similar problem and tried to implement your script above but had issues - would you look at http://stackoverflow.com/questions/37958644/unity-c-trying-to-randomly-choose-from-prefabs-in-queue-to-recycle-error-wit
Answer by zaid87 · Aug 15, 2014 at 09:16 AM
A simple way that I can think of is
Make an array of prefabs
Randomise a number between 1-4
Use the number to select which prefab to use
How do you implement that into the code that I have posted? I haven't really worked with arrays, so I'm a bit confused on how to go about adding that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160633im_/https://answers.unity.com/themes/thub/images/avi.jpg)