Instantiate random prefabs without duplication.

Hey guys.
Im trying to create a simple system to randomly generate a level from a bunch of prefabs i have made. i have a simple script that is attached to empty game objects spaced 40 units apart along the Z axis. Once i figure this out i will use it to generate more complex levels with corner peices and “T” connectors, but for simplicity sake lets just pretend the level just goes straight along the Z axis. This script is attached to an empty game object and it spawns a prefab that is just a room with 2 doors, perfectly centered, i can repeat this process to keep placing my rooms along the Z axis:

var node1 : Transform;
var block : GameObject;

function Start () {
spawnBlock();
}

function spawnBlock () {
Instantiate(block,node1.transform.position,node1.transform.rotation);
}

So i have 10 empty game objects with this script in a row, but how can i rig it so that it will spawn different prefabs (same size, just different geometry) without a chance of using the same room twice?

Hey,

you gonna need a List of your gameObjects which u want to use ( drop prefrabs in editor in there):

using System.Collections.Generic;

class XYZ{
        public List<GameObject> prefabs = null;

and then you can spawnBlock with

private void spawnBlock(){
        int count = prefabs.Count; // Get remaing prefabs count
        if( count == 0) {
               // Handle somehow this event
              return;
        }
        int i = Random.Range(0, count - 1); // choose random element
        GameObject go = prefabs[i]; // Get element from list
        prefabs.Remove(go); // Remove it from list (avoid duplication)
        Instantiate(go); // use it
}
using System.Collections.Generic;
using System.Linq;

...

    public List<GameObject> ObjectsToInstatiate;

    void Start()
    {
        ObjectsToInstatiate.OrderBy(obj => Random.Value).ToList().ForEach(obj => Instantiate(obj));
    }