Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 losingisfun · Jan 03, 2017 at 01:10 PM · castcasting

C# - Resources.Load Sprite trouble

For some reason I can't use Resources.Load to get sprites. The file I'm trying to get is a .png file. I have never had problems with them before.

This is what i'm doing:

 Sprite img = (Sprite)Resources.Load ("Sprites/inventoryImages/itemImages/sword");

This is the error I'm getting:

InvalidCastException: Cannot cast from source type to destination type.

is there someway i need to prep the file for this function?

Comment
Add comment · Show 4
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 Masterio · Jan 03, 2017 at 02:09 PM 0
Share

Create "Resources" folder then place the "Sprites" folder inside.

avatar image losingisfun Masterio · Jan 03, 2017 at 02:10 PM 0
Share

I know how to use Resources.Load, i use it all the time, its just not working when i cast (Sprite) and try to get a .png

avatar image rabirland losingisfun · Jan 03, 2017 at 02:33 PM 0
Share

Is the image is set to "Sprite" ins$$anonymous$$d of "Texture"?

Show more comments

2 Replies

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

Answer by $$anonymous$$ · Jan 03, 2017 at 06:29 PM

i use this to load sprite:

 skillSprite = Resources.Load<Sprite>("Sprites/CorePrototype/Arcanist Icons/" + SkillSpriteName);

Dont know why do you need (Sprite).

Comment
Add comment · Show 4 · 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 losingisfun · Jan 04, 2017 at 02:09 AM 0
Share

Thanks, that worked. I guess i made a habit of using (AudioClip)Resources.Load to cast. If you make that into an answer ill accept it for you.

avatar image lulitd · Sep 21, 2017 at 07:32 PM 0
Share

Thanks your answer helped me out. I was creating scriptable objects from a parsed csv... and could not for life of me figure out why my textures would load but not my sprites...

I was doing...

 iconSprite = Resources.Load(path) as Sprite;

I've learnt my lesson and not to cast resources!

avatar image Z3RP · May 11, 2018 at 10:14 AM 0
Share

Still not working for me what do i wrong ?

I tried:

 BTNSprites.Populate(Resources.Load("/Assets/W3 Tower/Defence/Images/dafault"));
 BTNSprites.Populate(Resources.Load<Sprite>("Assets/W3 Tower Defence/Images/dafault"));
 BTNSprites.Populate(Resources.Load<Sprite>("/W3 Tower Defence/Images/dafault"));
 BTNSprites.Populate(Resources.Load<Sprite>("W3 Tower Defence/Images/dafault"));

The Image is called "default" And this is the folder path : Unity\Tower defence\Assets\W3 Tower Defence\Images

avatar image kamaldeep26594 · Aug 29, 2020 at 01:30 PM 0
Share

Also Resources.Load searches for filenames ignoring extensions, in my case changing "cheese.png" to "cheese" worked.

avatar image
3

Answer by zly · Jan 04, 2017 at 08:57 AM

Loading Resources at Runtime

You need to create a folder named Recources

"Resource Folders

Resource Folders are collections of assets that are included in the built Unity player, but are not necessarily linked to any GameObject in the Inspector.

To put anything into a Resource Folder, you simply create a new folder inside the Project View, and name the folder “Resources”. You can have multiple Resource Folders organized differently in your Project. Whenever you want to load an asset from one of these folders, you call Resources.Load()."

(Unity Manual) I hope it helps. ;-D
Comment
Add comment · Show 1 · 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 losingisfun · Jan 04, 2017 at 10:39 AM 0
Share

you should have read the comments

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

66 People are following this question.

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

Related Questions

How to cast/convert a ControllerColliderHit into a Prefab 2 Answers

Is There A Way To Cast On 1 Returning Value 2 Times? 5 Answers

Casting Object to Rigidbody 2 Answers

Cannot cast from source type to desination type 0 Answers

Upcasting via GetComponent<>() 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