- Home /
Cant access the Image icon from class
So When i press play im getting only the white background but not the item icon that is supposed to be on it. My theory is that for some reason I cant access the Sprites in Items folder, but i don't really know what's the real problem.
I Couldn't upload the third screen of the code and thought they are all important to my question so i copied and pasted the last one.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class UIItem : MonoBehaviour { public Item item; private Image spriteImage;
private void Awake()
{
spriteImage = GetComponent<Image>();
//One slot at the start for the testing purposes
UpdateItem(null);
}
//Hiding an item
public void UpdateItem(Item item)
{
this.item = item;
if(this.item != null)
{
spriteImage.color = Color.white;
spriteImage.sprite = this.item.icon;
Debug.Log("if");
}
else
{
spriteImage.color = Color.clear;
Debug.Log("else");
}
}
}
alt text
Answer by unity_uNEXptjrFwHPwA · Sep 03, 2020 at 07:56 PM
Fixed it using System.Collections; using System.Collections.Generic; using UnityEngine; public class Item { public int id; public string title; public string description; public Sprite icon; public Dictionary<string, int> stats = new Dictionary<string, int>(); public Item(int id, string title, string description, Dictionary<string, int> stats) { this.id = id; this.title = title; this.description = description; this.icon = Resources.Load<Sprite>("Sprites/Items/" + title); this.stats = stats; } public Item(Item item) { this.id = item.id; this.title = item.title; this.description = item.description; this.icon = Resources.Load<Sprite>("Sprites/Items/" + item.title); this.stats = item.stats; } }
Your answer
Follow this Question
Related Questions
Cant acces an image icon from class 0 Answers
Code in void not executing 0 Answers
Rabit movement animation and coding problem 1 Answer
My Algorithm for checking whole tiles didint work 0 Answers
Second Coroutine isn't working Unity 1 Answer