Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by viciousesque · Oct 09, 2015 at 10:11 AM · scripting problemscript error

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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");
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 09, 2015 at 02:45 PM 1
Share

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.

avatar image meat5000 ♦ Bunny83 · Oct 09, 2015 at 03:12 PM 0
Share

As usual, your supporting comment is an answer in itself. +1

avatar image viciousesque meat5000 ♦ · Oct 09, 2015 at 10:54 PM 0
Share

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;}
avatar image
0

Answer by smartether · Oct 09, 2015 at 02:08 PM

try Resources.LoadAll("Textures");

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges