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.

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.