Search Unity

Changing UI sprite

Discussion in 'Getting Started' started by killerKeks26601, Mar 17, 2021.

  1. killerKeks26601

    killerKeks26601

    Joined:
    Jan 13, 2021
    Posts:
    31
    Hello,
    I have created an inventory system and most of it seems to be working fine.
    I just have one problem when i instantiate the item slot.
    The script on the slot object should enabled its image component and change its sprite to the item icon.
    It enabled and disables just fine but instead of loading the sprite it instead just loads a blank white square.
    I will post the script of the UIItem and the Itemclass where it stores the resource path.

    Code (CSharp):
    1.  public void UpdateItem(Item item)
    2.     {
    3.         this.item = item;
    4.         if(this.item != null)
    5.         {
    6.             spriteImage.enabled = true;
    7.             spriteImage.sprite = this.item.icon;
    8.         }
    9.         else
    10.         {
    11.             spriteImage.enabled = false;
    12.         }
    Code (CSharp):
    1. public class Item
    2. {
    3.     public int id;
    4.     public string type;
    5.     public string title;
    6.     public string description;
    7.     public Sprite icon;
    8.     public Dictionary<string,int> stats = new Dictionary<string,int>();
    9.    
    10.     public Item(int id,string type, string title, string description, Dictionary<string,int> stats)
    11.     {
    12.         this.id = id;
    13.         this.type = type;
    14.         this.title = title;
    15.         this.description = description;
    16.         this.icon = Resources.Load<Sprite>("Sprite/Items/" + type + "/" + title);
    17.         this.stats = stats;
    18.     }
    19.     public Item(Item item)
    20.     {
    21.         this.id =  item.id;
    22.         this.type = item.type;
    23.         this.title = item.title;
    24.         this.description = item.description;
    25.         this.icon = Resources.Load<Sprite>("Sprites/Items/" + item.type + "/" + item.title);
    26.         this.stats = item.stats;
    27.     }
    28. }
     
  2. killerKeks26601

    killerKeks26601

    Joined:
    Jan 13, 2021
    Posts:
    31
    PS: there are some functions inbetween that instantiate the slot prefab and create and store some Item Lists.
    But those should be fine i think since everything including some Debug.Logs work.
    Like i said it even enables/disables the right item slots after instantiating it just doesnt load the sprite.

    Also i included a screenshot of my resources folder.
     

    Attached Files:

  3. killerKeks26601

    killerKeks26601

    Joined:
    Jan 13, 2021
    Posts:
    31
    nevermind i solved the problem turns out i had a little typo in my code