So I’m trying to instantiate a random prefab from a folder full of prefabs. I’ve done a ton of reading and just cant figure it out. I think I am referring to the folder with my prefabs in it improperly. My prefabs are in Assets/Resources/Prefabs/Hallways. What am I doing wrong? here’s my code. P.S. any other optimization tips would be greatly appreciated.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class HallScript : MonoBehaviour {
private GameObject target;
[SerializeField]
private float checkPoint;
[SerializeField]
private float position;
public GameObject hallPrefab;
private GameObject prefabClone;
private List<GameObject> HallList;
public GameObject[] HallListArray;
// Use this for initialization
void Start ()
{
checkPoint = 0;
target = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update () {
position = gameObject.transform.position.z;
gameObject.transform.position = new Vector3(0,0, target.transform.position.z);
if (position >= checkPoint)
{
checkPoint = checkPoint + 40;
spawnHall();
}
}
void spawnHall()
{
print("Parameter Met");
HallListArray = Resources.LoadAll<GameObject>("HallPrefabs");
HallList = HallListArray.ToList();
hallPrefab = HallList[Random.Range(0, HallList.Count)];
prefabClone = Instantiate(hallPrefab, new Vector3(0,0, checkPoint), Quaternion.identity) as GameObject;
}
}