I’ve got 15 prefabs. and i would like them to generate randomly above each other, lets say every 3 seconds, I’ve got this script, but it generates one on top another, i need to generate a prefab above the last prefab, like .y+1
public GameObject[] obj;
// Use this for initialization
void Update () {
}
void Switch(){
Instantiate (obj [Random.Range(0, obj.Length)], transform.position, Quaternion.identity);
}
//drag your prefabs into this public array from the inspector
public GameObject myPrefabs;
IEnumerator generatePrefabs()
{
int currentY = 0;
while (currentY < myPrefabs.Length)
{
Instantiate(myPrefabs[currentY],new Vector3(<your X value>,<your Y Value> + currentY,<your Z Value>),Quaternion.identity);
currentY++;
yield return new WaitForSeconds(3f);
}
}
In some function … that triggers these to start generating, add the following line:
StartCoroutine(“generatePrefabs”);
If you want the prefabs to be generated in a random order, then you’ll have to add some more logic to ‘tick them off’ as selected. Thus an array the same size as your myPrefabs array, that just holds a ‘used’ or ‘not used’ value. Generate a random number, if it’s not used, instantiate it and mark as used. If used, generate another number until you have an unused prefab spot.