- Home /
Problem with getting resources and adding to list. (c#)
Hello, I'm having some trouble with what I hoped to be a reasonably easy piece of code. My intention is for it set a GameObject variable to be a file ending in .prefab from a resources folder and then changing the name to be a number before adding it to a list and increasing said number until there's no remaining GameObjects in the folder.
For some reason, it doesn't want to work properly - but I'm getting no errors, so I have no clue what's gone wrong.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PrefabSetup : MonoBehaviour {
public static List<GameObject> prefabRooms = new List<GameObject>();
public static int prefabCount = 0;
void Start () {
int objectNumber = 0;
string objectNumberAsString;
bool prefabsLoaded = false;
while (prefabsLoaded == false) {
GameObject currentObject = Resources.Load("*.prefab") as GameObject;
System.Threading.Thread.Sleep (50);
if (currentObject == null) {
Debug.Log ("currentObject = null");
prefabsLoaded = true;
} else {
Debug.Log (currentObject.ToString ());
objectNumberAsString = objectNumber.ToString ();
currentObject.name = objectNumberAsString;
Debug.Log ("Set To: " + currentObject.ToString ());
prefabRooms.Add (currentObject);
objectNumber++;
prefabCount++;
}
}
}
void Update () {
}
}
Any help would be appreciated. The reason I'm doing it this way and not simply doing a GameObject array with a Resources.LoadAll is I eventually want to have a progress bar or loading percentage and I want it to visibly go through every file and load it one after the other.
Never seen that you can pass "*.prefab"
to Resources.Load. Does that actually work or is this what is going wrong for you?
Answer by flaviusxvii · Dec 10, 2013 at 11:39 PM
So I'm pretty sure this:
Resources.Load("*.prefab")
can't possibly work the way you intend. That'll return null right off the bat. You'll get "currentObject = null" in your console, and then the loop is over.
http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted.
There's no reason to think it would respect that wildcard syntax.
So how can I load a resource that can have any name but must be a prefab? Googling it isn't helping me.
Alright well I'm going to try a different method, thanks for the help.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
A node in a childnode? 1 Answer
can somebody help for the algorithm or code how implement this crossword math? 1 Answer
Combine duplicates in a collection of class objects 0 Answers