- Home /
Populate an array of prefabs and then load them
I am trying to load an array of prefabs, and then instantiate them when I need.
What am I doing wrong?
private var roadAssets:Array = new Array();
private var platform:GameObject;
function Start () {
roadAssets = Resources.LoadAll("Assets/Prefabs/Road",GameObject);
platform = roadAssets[0] as GameObject;
newPlatform = Instantiate(platform,Vector3(0,0,0),Quaternion(0,0,0,0));
}
Thank you
Answer by hiddenspring81 · May 24, 2013 at 12:21 AM
The way that the Resources methods work, and by extension, Resources.LoadAll(), is that Unity will crawl through your project, and find all the directories named "Resources". Then, each time you call a Resources method, it will attempt to load the asset, at the path you specify, relative to any Resources directory.
For example, when you call Resources.LoadAll("Assets/Prefabs/Road"), Unity will load all the Prefabs inside of a directory, located at "Resources/Assets/Prefabs/Road", which I don't think is what you meant to do. To load the Prefabs using Resources, just move your "Prefabs/Road" directory inside of a new directory named "Resources", and load them using Resources.LoadAll("Prefabs/Road").
Thank you for this explanation! Relocating the Prefabs into the Resources folder fixed a 3 day long headache!!!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Instantiate prefab 1 Answer
Create a Prefab from imported Assets automatically 0 Answers
Pressing Apply Breaks Prefab 1 Answer
How to find the prefab base of a Prefab Variant through code? 1 Answer