So I have this code and for now it doesn't show any images, because something doesn't work with Resources.Load<Texture2D>("Assets/Sprites/Item Icons/Wanderer Crate/" + name);
and when I run code it doesn't enter Item Icon and this makes me mad for 3 straight days... There is no correct answer on Stack Overflow for my code... So what could be it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Item {
public string itemName;
public int itemID;
public float itemPrice;
public Texture2D itemIcon;
public ItemType1 itemCrate;
public enum ItemType1
{
Wanderer_Crate
}
public ItemType2 itemType;
public enum ItemType2
{
Key,
Mask,
Pants,
Hat,
Shoes,
Shirt,
Glasses,
Jacket
}
public ItemType3 itemRarity;
public enum ItemType3
{
Red,
Purple,
Violet,
Blue,
Green,
Gray
}
public Item(string name, int id, float price, ItemType1 crate, ItemType2 type, ItemType3 rarity)
{
itemName = name;
itemID = id;
itemPrice = price;
itemIcon = Resources.Load<Texture2D>("Assets/Sprites/Item Icons/Wanderer Crate/" + name);
itemCrate = crate;
itemType = type;
itemRarity = rarity;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public static Inventory Instance { set; get; }
public List<Item> inventory = new List<Item>();
public List<Item> slots = new List<Item>();
public GameObject slotPreset;
public GameObject parentInvetoryGridForSlots;
public Text itemNameText;
private ItemDatabase database;
public void Awake()
{
Instance = this;
}
void Start()
{
database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
//Add item to invetory code - inventory.Add(database.Items[16]);
inventory.Add(database.Items[16]);
inventory.Add(database.Items[3]);
inventory.Add(database.Items[4]);
inventory.Add(database.Items[5]);
inventory.Add(database.Items[6]);
inventory.Add(database.Items[7]);
for (int i = 0; i < inventory.Count; i++)
{
GameObject spawnedSlot = Instantiate(slotPreset);
spawnedSlot.transform.SetParent(parentInvetoryGridForSlots.gameObject.transform);
spawnedSlot.GetComponentInChildren<Text>().text = inventory[i].itemName;
}
}
public void inventoryRefresh(int itemIDofItem)
{
GameObject spawnedSlot = Instantiate(slotPreset);
spawnedSlot.transform.SetParent(parentInvetoryGridForSlots.gameObject.transform);
spawnedSlot.GetComponentInChildren<Text>().text = database.Items[itemIDofItem].itemName;
}
public void addItemToInventory(int itemIDofItem)
{
inventory.Add(database.Items[itemIDofItem]);
}
}
If you take a look at Unitys' documentation (https://docs.unity3d.com/ScriptReference/Resources.Load.html), it states ...
Resources.Load
:: Loads an asset stored at path in a Resources folder.
I'd guess the problem is located here. Make sure the parent folder of "Assets/Sprites/Item Icons/Wanderer Crate/" is a folder with the name "Resources".