Hi, I have this simple script which spawns a prefab every 5 seconds. I would like to add a randomize to it.
I have 8 prefabs and I want to spawn 1 of the 8 prefabs at random every 5 seconds. I have tried adding other bits of code to it but can’t get it to work.
My Code so far:
var prefabs : GameObject;
var timeDelay = 5;
function Start() {
while (true) {
yield WaitForSeconds(timeDelay);
Instantiate( prefabs, transform.position, Quaternion.identity );
}
}
public var prefabs : GameObject[] = new GameObject[8];
public var timeDelay = 5;
function Update() {
if (Input.GetKeyDown(KeyCode.P))
SpawnPrefabs();
}
function SpawnPrefabs () {
Instantiate(prefabs[Random.Range(0,prefabs.length)], transform.position, Quaternion.identity);
yield WaitForSeconds(timeDelay);
}
I haven’t tested it though but you could give it a try.
Thanks a lot, much appreciated.
Final script to how i wanted it.
#pragma strict
public var prefabs : GameObject[ ] = new GameObject[8];
var timeDelay = 5;
function Start() {
while (true) {
yield WaitForSeconds(timeDelay);
Instantiate(prefabs[Random.Range(0,prefabs.length)], transform.position, Quaternion.identity);
}
}