- Home /
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?
Create "Resources" folder then place the "Sprites" folder inside.
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
Is the image is set to "Sprite" ins$$anonymous$$d of "Texture"?
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).
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.
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!
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
Also Resources.Load searches for filenames ignoring extensions, in my case changing "cheese.png" to "cheese" worked.
Answer by zly · Jan 04, 2017 at 08:57 AM
You need to create a folder named Recources
(Unity Manual) I hope it helps. ;-D"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()."
Your answer
Follow this Question
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