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
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")
.