- Home /
Inventory image not showing up
I have made a inventory system but when i open the inventory, i just see the slots, not the items. What do i do? Here are the scripts:
Item:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Item {
public string itemName;
public int itemID;
public string itemDesc;
public Texture2D itemIcon;
public int attackDMG;
public int attackSpeed;
public int def;
public int manaUse;
public ItemType itemType;
public enum ItemType {
MeleeWeapon,
Consumable,
Block,
Crafting,
Gun,
Spell,
Helmet,
Chestplate,
Leggins,
Boots
}
public Item(string name, int id, string desc, int DMG, int speed, int manaUseage, ItemType type) {
itemName = name;
itemID = id;
itemDesc = desc;
itemIcon = Resources.Load<Texture2D>("Item Icons/ " + name);
attackDMG = DMG;
attackSpeed = speed;
manaUse = manaUseage;
}
public Item(){
}
}
ItemDatabase:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ItemDatabase : MonoBehaviour {
public List<Item> items = new List<Item>();
}
And finally, Inventory:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour {
public int slotsX, slotsY;
public List<Item> inventory = new List<Item>();
public List<Item> slots = new List<Item>();
private ItemDatabase database;
public bool showInventory = true;
public GUISkin skin;
// Use this for initialization
void Start () {
for (int i = 0; i < (slotsX * slotsY); i++) {
slots.Add (new Item());
inventory.Add (new Item());
}
database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
AddItem (0);
print (InventoryContains(0));
print (InventoryContains(1));
}
void Update(){
if (Input.GetKeyDown(KeyCode.I)){
showInventory = !showInventory;
}
}
void OnGUI(){
for (int i = 0; i < inventory.Count; i++)
{
GUI.skin = skin;
}
if(showInventory){
DrawInventory();
}
}
void DrawInventory() {
int i = 0;
for (int x = 0; x < slotsX; x++){
for (int y = 0; y < slotsY; y++){
Rect slotsRect = new Rect(x * 60, y * 60, 50, 50);
GUI.Box(slotsRect, "" , skin.GetStyle("slot"));
slots[i] = inventory[i];
if (slots[i].itemIcon != null) {
GUI.DrawTexture (slotsRect, inventory[i].itemIcon);
}
i++;
}
}
}
void AddItem(int id) {
for (int i = 0; i < inventory.Count; i++){
if (inventory[i].itemName == null){
for (int j = 0; j < database.items.Count; j++){
if (database.items[j].itemID == id){
inventory[i] = database.items[j];
}
}
break;
}
}
}
bool InventoryContains (int id){
bool result = false;
for (int i = 0; i < inventory.Count; i++){
result = inventory[i].itemID == id;
if (result) {
break;
}
}
return result;
}
}
Images:
The hierarchy:
The item database:
The inventory object:
What appears:
What SHOULD appear (I edited the image):
Edit: Now, when i open the inventory i get a single inventory square and get spammed with an error saying something on the lines of "Argument is out of range"
Edit Edit: I don't get the error(s) anymore, now the image just doesn't show up. I've tried adding the item by index creating a test item. So it seems that it's something that's wrong with the DrawInv function.
Are there any error messages stopping the script's execution?
If not, are the textures being loaded properly? (i.e. line 50, is it deter$$anonymous$$ing the texture to be null?)
At any rate, is the first slot, for instance, properly receiving data on the sword in the first place? If not, do any of the slots technically have the data for it?
Yes, in the inventory script there's a "InventoryContains" function, and that says that i have the sword. The only problem is that there is no texture. I don't get any errors/warnings when i start the game nor when i open the inventoryDo any of the slots technically have the data for it?
Have you solved your problem? Because:
itemIcon = Resources.Load<Texture2D>("Item Icons/ " + name);
There is a space after the "/". Is this intentionally?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can not retrive image form device to the plane at runtime. 0 Answers
Texture2D.Resize and Shader 0 Answers
What is an element in CopyTexture ? 1 Answer