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 forlength’ and no extension methodlength' of typeUnityEngine.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 convertobject’ 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;
}
}