Picking a random prefab to instantiate

I am trying to get 4 prefabs to spawn and not have 2 of the same spawn in a row, the errors I keep getting are

  • MissingReferenceException: The object of type ‘Object’ has been destroyed but you are still trying to access it.

  • Type UnityEngine.GameObject[ ]' does not contain a definition for length’ and no extension method length' of type UnityEngine.GameObject[ ]’ could be found (are you missing a using directive or an assembly reference?)

  • error CS1502: The best overloaded method match for `UnityEngine.Random.Range(float, float)’ has some invalid arguments

  • error CS1503: Argument #2' cannot convert object’ expression to type `float’

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TileManager : MonoBehaviour {

    public GameObject[] tilePrefabs;
    private Transform playerTransform;
    private float spawnZ = 0.0f;
    private float TileLength = 24.62f;
    private int amnTilesOnScreen = 10;
    private float safeZone = 300f;
    private int lastPrefabIndex = 0;

    private List<GameObject> activeTiles;

    // Use this for initialization
    private void Start () {
        activeTiles = new List<GameObject> ();
        playerTransform = GameObject.FindGameObjectWithTag ("Player").transform;

        for (int i = 0; i < amnTilesOnScreen; i++) {
            SpawnTile ();
        }
    }
   
    // Update is called once per frame
    private void Update () {
        if (playerTransform.position.z - safeZone> (spawnZ - amnTilesOnScreen * TileLength)) {
            SpawnTile ();
            DeleteTile ();
        }
   
    }
    private void SpawnTile(int prefabIndex = -1)
    {
        GameObject go;
        go = Instantiate (tilePrefabs [RandomPredabIndex()]) as GameObject;
        go.transform.SetParent (transform);
        go.transform.position = Vector3.forward * spawnZ;
        spawnZ += TileLength;
        activeTiles.Add (go);
    }
    private void DeleteTile()
    {
        Destroy (activeTiles [0]);
        activeTiles.RemoveAt (0);
    }
    private int RandomPredabIndex(){
        if (tilePrefabs.Length <= 1)
            return 0;
       
        int randomIndex = lastPrefabIndex;
        while (randomIndex == lastPrefabIndex) {
            randomIndex = Random.Range (0, tilePrefabs.length);
        }
        lastPrefabIndex = randomIndex;
        return randomIndex;
    }
}

error 1) line 47? you’re already set that array element to be destroyed so how are you going to remove what no longer exists.

error 2) line 55; length should be Length

error 3) extension of error 2, it’s getting confused about the second argument, sort that and it should automatically find the (int, int) overload

error 4) another extension of 2

should also point out that for any array the length is going to be one higher than the maximum index since the index starts at 0, so it should always be 0 → Length -1 not 0-> Length (you’ll have to check the random.range function overloads, can never remember which one is inclusive and which is exclusive :P)