- Home /
Load sprite to UI.Image.sprite
I'm trying to load a sprite to my Ui.Image but for some reason i'm not getting any errors or results. I'm not sure how to debug this issue.
public GameObject DisplayImage;
void OnClick( int id ){
DisplayImage.GetComponent<Image>().sprite = Resources.Load("Assets/CharacterImages" + results[id].ImgName) as Sprite; //results[id].ImgName = "/Humans/Soldier"
}
I'm not sure what path works so i've tried the following already and none of them work.
Assets/CharacterImages/Humans/Soldier
../CharacterImages/Humans/Soldier
/CharacterImages/Humans/Soldier
CharacterImages/Humans/Soldier
Appreciate it if anyone can point me in the direction to figure this out. Thanks.
Answer by RLin · Jul 22, 2018 at 03:40 PM
Resources.Load only works for objects placed in the resources folder. To do this, create a folder called “Resources” and place all of your sprites inside of it. Then, change the resources call to Resources.Load(results[id].imgName) as Sprite;
Resources.Load does NOT take the path of an asset into account. All it does is search within all folders called resources for an Asset with the given name.
Click here for more information.
Unity is not letting me create a Resource folder outside of Assets. so the full path would be
Assets/Resources/CharacterImages/Humans/Soldier
but Resources.Load only requires "/CharacterImages/Humans/Soldier"?
That is correct. Resources folders can be anywhere in the assets folder. When you use Resources.Load, you do NOT use the path of the object; ins$$anonymous$$d, you only use the file name of the asset without extensions. See the code I posted in my answer.
so if the full path is Assets/Resources/CharacterImages/Humans/Soldier
i need the filename without extension like "Soldier" or "Humans/Soldier"? because they both didnt work.
Put the soldier directly into the resources folder, so the full path is Assets/Resources/Soldier or even Assets/CharacterImages/Humans/Resources/Soldier. Then only use “Soldier” when loading it.
Your answer
Follow this Question
Related Questions
iPhone- import and use images from device 1 Answer
Save a downloaded image 1 Answer
Cycle through images on click c# 2 Answers
Beginner Question Regarding Sprites w/Multiple Images 0 Answers