- Home /
Resources.LoadAll Generates Cast exception Error
I have a directory under Assets/Resources named "Textures". It contains two imported image files (.jpg, .png). If I view each of these files in the inspector they appear to be textures. I use them as textures on materials, so I'm pretty sure they are indeed textures.
Using the code snippet directly from the documentation:
Texture2D[] textures = (Texture2D[]) Resources.LoadAll("Textures")
Returns the following error:
"InvalidCastException: Cannot cast from source type to destination type."
I have tried changing things about that line of code, such as switching "Texture2D[]" to "Texture[]"; adding "as Texture[]" to the end of the statement; removing the "(Texture2D[])" token. The best I can achieve is to get rid of the error by removing the "(Texture2D[])" token AND adding "as Texture2D[]" to the end of the statement, then I no longer generate the error, but my object is null.
HELP!
Answer by Chris-Paul · Oct 09, 2015 at 12:45 PM
You can use this, which solves the casting problem for you:
Texture2D[] textures = Resources.LoadAll<Texture2D>("Textures");
Just want to add why this error actually happend,
Resources.LoadAll()
can load any resources that are inside the specified folder. So no matter if there are textures, models, prefabs, audioclips, materials, ... in that folder, they all will be loaded by this method. Since all those resources would be loaded if they exist, the method can't return a Texture2D array.
It has to use a type that all those types share which is UnityEngine.Object. So it actually returns an "Object[]". That can't be up casted to a Texture2D (even when all elements are actually Texture2D).
Resources.LoadAll<T>()
on the other hand has a generic type parameter It will only load resources which are "castable" to the given type. Therefore the method returns an array that has the given type as element type.
For example if you use Resources.LoadAll<Texture>()
(note: Texture, not Texture2D) it will return all resources which are Textures. So all Texture2D's but also Texture3D and RenderTextures. The returned array will be of type Texture[] which also can't be casted to a Texture2D array since it still can contain other things than Texture2Ds.
As usual, your supporting comment is an answer in itself. +1
Thanks! That worked!
Here it is in context:
Sprite[] image_sprites = Resources.LoadAll<Sprite>("GalleriesImages/Gallery_1");
I can then successfully set new sprites on arbitrary collections of UI.Image objects, like so:
foreach(Sprite i_s in image_sprites){
GameObject ic = Instantiate (Resources.Load ("Prefabs/ImageCanvas")) as GameObject;
Image img = ic.GetComponent<Image>();
img.sprite = i_s;}
Your answer
Follow this Question
Related Questions
Movement Script stops working 0 Answers
Having trouble using SendMessage 1 Answer
How to control ios statusbar with script? 1 Answer
How can i set transform.position on y ? And why i'm getting error cannot convert double to float ? 3 Answers
Object flashing in and out every frame (because of my script) 0 Answers