lNeves
1
This is my script:
using UnityEngine;
using System.Collections;
public class Spwn: MonoBehaviour {
public GameObject[] obj;
public float spawnMin = 1f;
public float sapwnMax =2f;
void Start (){
Spawn();
}
void Spawn (){
Instantiate(obj[Random.Range (0,obj.GetLength(0))], transform.position, Quaternion.identity);
Invoke ("Spawn", Random.Range (spawnMin, sapwnMax));
}
}
Is there a way that some game objects have more chances to spawn them others ?
One approach:
void Spawn (){
// Get a random number from 0 - 100.
float rand = Random.Range(0f, 100f);
if(rand < 30) {
// 30% chance of creating obj[0]
Instantiate(obj[0], transform.position, Quaternion.identity);
}
else if(rand < 80) {
// 50% chance of creating obj[1]
Instantiate(obj[1], transform.position, Quaternion.identity);
}
else {
// 20% chance of creating obj[2]
Instantiate(obj[2], transform.position, Quaternion.identity);
}
Invoke ("Spawn", Random.Range (spawnMin, sapwnMax));
}