- Home /
UI Image source image not changing
So I'm creating an inventory system for my game, and I'm having an issue with the image not updating.
So I have a class with a constructor
 public Item(string slug)
     {
         this.Slug = slug;
         this.Sprite = Resources.Load<Sprite> ("Art/" + slug);
     }
and this function
 public void AddItem (int id)
     {
         Item itemToAdd = database.getItemByID (id); //gets item from database
         for (int i = 0; i < items.Count; i++) 
         {
             if(items[i].ID == -1)
             {
                 items[i] = itemToAdd;
                 GameObject itemObject = Instantiate(inventoryItem);
                 itemObject.transform.SetParent(slots[i].transform);
                 itemObject.GetComponent<Image>().sprite = itemToAdd.Sprite;
                 itemObject.transform.position = Vector2.zero;
                 break;
             }
         }
     }
But when I run and debug my game, the image is blank and has not loaded.
From what I have found it is an issue with the Image Source not loading the image and staying null, which means the this.Sprite = Resources.Load<Sprite> ("Art/" + slug); line is not working in the constructor.
Any help would be appreciated!
Answer by Marceta · Dec 12, 2016 at 07:30 AM
Try this:
 this.Sprite = Resources.Load<Sprite> ("Art/" + slug) as Sprite;
Edit:
 Object [] slices;
 slices = Resources.LoadAll ("YourSpriteSheet");
 renderer.sprite = (Sprite)slices [3]; //So now i got 3rd slice from my sprite sheet.
This doesn't work either sadly. It's still not setting the Source Image on the Image UI to it :/
Then something else is wrong, since that is working 100%, i just tested it. It load's sprite from resources.
Check if that particular image is set as "Sprite (2D and UI) on Texture Type drop down inside import settings. https://s18.postimg.org/6g49veq09/Screen_Shot_2016_12_12_at_8_40_07_A$$anonymous$$.png
Oh so you have sliced sprite sheet. Ok i edited my answer and posted you solution.
So I had to fiddle with the idea a lot, but I eventually got it working with this! Thanks! $$anonymous$$arking as answer now.
Answer by nj4242 · Dec 12, 2016 at 07:01 AM
Try relocating the "art" folder into "Resources". You need to create this folder in Assets, If you don't have one.
Also try avoiding "this". Create a public Gameobject variable, and use it like this as in constructor.
 public Gameobject mySprite;
 
 public Item()
 {
     mySprite = Resources.Load<Sprite> ("Art/" + slug);
 }
Thanks for the reply! I have already got the Art folder inside of the resources folder, so thats not part of the issue sadly.
"Also try avoiding "this". Create a public Gameobject variable, and use it like this as in constructor.". That is the dumbest thing I have ever heared.
Your answer
 
 
             Follow this Question
Related Questions
2D Image not appearing in game but only in scene 1 Answer
Unity to Check Arduino LED Status 0 Answers
Accessing RectTransform Rotation? 1 Answer
Saving image in a variable purely through code? 1 Answer
Distribute terrain in zones 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                