- Home /
Keep getting null when using Resource.load with a path.
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[i];
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)
Answer by robertbu · Dec 11, 2013 at 05:58 PM
You have a couple of problems here. First, GetFiles() returns a full path. Resources.Load() needs a relative path to the 'Resources' folder. So you will need to do some processing on the strings returned by GetFiles() to have something appropriate for Resources.Load(). The second problem is that the 'Resources' folder does not exist in a build. So if you are tying to run this script in anything but the editor it will fail.
Assuming you need a runtime solution, you could make prefabFiles a public class variable and write an editor script that populates the list in the editor. At runtime, you would then just have to cycle through the list.
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 ins$$anonymous$$d, 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?
Resources.Load() is fine. It works in a build outside the editor as long as the files you are adding are at edit time. GetFiles()does not work because the files inside Resources.Load() get folded into a binary file. So What I'm suggesting is that you create an editor script that processes "/Resources/Prefab_Rooms/" and produces an array of names for the game objects to be loaded. These strings will be serialized and be available in the build, so you can then load them in a loop and report the progress to the user. Of course this will not work if you are trying to allow user to add files at the runtime of a build.
Well 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.
$$anonymous$$y coding knowledge isn't great, but this will hopefully be good for learning.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Set Current Array GameObject to Active 1 Answer
How to check for an empty array? 1 Answer