Specifically, the path is taken from an Array. I’m not an expert on C# so I can’t easily see what is wrong here;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PrefabSetup : MonoBehaviour {
public static List<GameObject> prefabRooms = new List<GameObject>();
string resourcesFolder;
string resourcesFilter = "*.prefab";
int objectNumber = 0;
string objectNumberAsString;
GameObject currentObject = null;
void Start () {
// Sets resourcesFolder to be the correct path.
resourcesFolder = Application.dataPath + "/Resources/Prefab_Rooms/";
}
void Update () {
// Creates the prefabFiles array.
string[] prefabFiles = System.IO.Directory.GetFiles(resourcesFolder, resourcesFilter);
for (int i = 0; i < prefabFiles.Length; i++) {
string path = prefabFiles*;*
-
currentObject = Resources.Load(path) as GameObject;* -
Debug.Log (currentObject.ToString ());* -
objectNumberAsString = objectNumber.ToString ();* -
currentObject.name = objectNumberAsString;* -
Debug.Log ("Set To: " + currentObject.ToString ());* -
prefabRooms.Add (currentObject);* -
objectNumber++;* -
}* -
}*
}
What I’m trying to do is make it so there’s a “Prefab_Rooms” folder that you can simply drop .prefab files into and then when the game is run, the code will get each file and add them to a list which will be used throughout the game. I want to do this as easily as possible, whilst still loading each file individually rather than use Resources.LoadAll (So that I can make it visible to the user that each file is being loaded successfully.)
Does anyone have any ideas what is going wrong and can you please help? I keep getting the NullReferenceException and I don’t understand why.
(This is similar to my previous question, but since that question I’ve changed the code but still to no avail)
Thank you for the help. I guess that - for what I'm trying to do - using Resources.load isn't such a good idea? I'm not sure what I could do instead, but I will look into some possibilities. I need it to work in builds outside of the editor. What exists in place of a resources folder? Do resources just get put into the _Data folder?
– TanksyWell not allowing them to add files whilst the build is running is fine, I wouldn't of wanted them too anyway. I just would like a way of allowing users to put prefab files into a folder which then get loaded and "set up" for use when the game is loaded. I think I understand what you're saying, so I'll try looking up ways to do it. My coding knowledge isn't great, but this will hopefully be good for learning.
– Tanksy