- Home /
Fill an array with textures from a folder?
I'd like to be able to automatically fill an array with Textures from a folder in the Start function, instead of having to define all of them by hand in the inspector. I heard you can access a folder called "Resources" from within the script? I'm using Javascript. Thanks in advanced!
Answer by dannyskim · Sep 29, 2012 at 04:43 PM
Well, if you look at the Unity Script Reference and do a search for 'Resources,' it should be pretty clear on how you're supposed to use it to populate variables in Start() with a Resources.Load() call.
http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html
Thanks, I read the scripting reference and tried using this: tex = Resources.LoadAll("Textures");
but it give me this error (and yes I made a folder named "Textures" inside the Resources folder, and placed all my textures inside of it):
InvalidCastException: Cannot cast from source type to destination type.
Edit: Oh wait, fixed it, just needed to change the array type to "Object" ins$$anonymous$$d of Texture. Thanks anyway! :D
Well I suggest you read up on what a cast exception is.
When you look at the link I provided, you'll also clearly see that Resources.LoadAll() returns an Object[], not a Texture[]. So this is where an explicit type case comes into play.
var myTexture : Texture2D = (Texture2D)Resources.Load("texture_name");
is an example of casting from Object to Texture2D.
Your answer
Follow this Question
Related Questions
How to read text file from resources and store into 2d array 3 Answers
Resources.LoadAll(); GameObject[] Array 1 Answer
Using Resources.LoadAll with arrays 1 Answer
Get array of name from file in resources 1 Answer
Resources Load array 0 Answers