Hello,
(C#, 2D game, Platformer)
I started working on a inventory system for the first time and I can already see issues with the way I script it.
I did know that my script would be bad by the way, but I wanted to give it a go just to learn and earn experience.
First problem I ran into: I don’t know loops, and then I wrote this to draw the slots. Which works but in the end I would love just making a easy loop to draw inventory slots ( the Background image for the slots)
Draw script
// Draw Inventory Slots:
//0,0
GUI.DrawTexture (new Rect (slot_x_pos + (slotXsize * 0), slot_y_pos + (slotYsize * 0), slotXsize, slotYsize), slot);
//0,1
GUI.DrawTexture(new Rect(slot_x_pos+(slotXsize*1),slot_y_pos+(slotYsize*0),slotXsize,slotYsize),slot);
//0,2
GUI.DrawTexture(new Rect(slot_x_pos+(slotXsize*2),slot_y_pos+(slotYsize*0),slotXsize,slotYsize),slot);
//0,3
GUI.DrawTexture(new Rect(slot_x_pos+(slotXsize*3),slot_y_pos+(slotYsize*0),slotXsize,slotYsize),slot);
//1,0
GUI.DrawTexture(new Rect(slot_x_pos+(slotXsize*0),slot_y_pos+(slotYsize*1),slotXsize,slotYsize),slot);
//1,1
GUI.DrawTexture(new Rect(slot_x_pos+(slotXsize*1),slot_y_pos+(slotYsize*1),slotXsize,slotYsize),slot);
For my database I tried two examples, but I see issues in both. Maybe I shouldn’t do it this way at all or create single array for each item, I don’t know.
Example 1
// 1 = Food
// 2 = Armor
// 3 = One-hander
// 4 = Shield
// 5 = Two-hander
public Texture[] Icon1;
[HideInInspector] public string[] Name1 = new string[6] {"Apple","Pie", "", "", "",""};
[HideInInspector] public string[] Desc1 = new string[6] {"Eat me","Eat me NOW", "", "", "",""};
public Texture[] Icon2;
[HideInInspector] public string[] Name2 = new string[6] {"Basic Leather Armor","Basic Cloth Armor", "", "", "",""};
[HideInInspector] public string[] Desc2 = new string[6] {"Armor Test","My skin is protected by nothing", "", "", "",""};
Example 2
[HideInInspector] public string[] Type = new string[6] {"Food","Armor", "One-hander", "Shield", "Two-hander};
[HideInInspector] public string[] Name = new string[6] {"Apple","Basic Leather Chest", "", ""};
[HideInInspector] public string[] Desc = new string[6] {"Eat Me","Test Armor", "", ""};
[HideInInspector] public int[] lvlReq = new int[6] {1,1,0,0,0,0};
[HideInInspector] public int[] Health = new int[6] {20,0,0,0,0,0};
[HideInInspector] public int[] Stamina = new int[6] {0,0,0,0,0,0};
[HideInInspector] public int[] Strength = new int[6] {0,5,0,0,0,0};
The last thing I started on was right clicking items in the inventory and grabbing/showing items in the inventory.
showing the item
if (slot00 == true) {
if (slot00_type == 1) {
if (GUI.Button (new Rect (slot_x_pos + (slotXsize * 0) + 4, slot_y_pos + (slotYsize * 0) + 4, iconXsize, iconYsize), item_database.Icon1 [slot00_value], "")) {
editing = true;
}
}
}
“Right click”
// Right clicked Fruit
if (editing)
{
for (int x = 0; x < 3; x++)
{
if (GUI.Button(new Rect(Box.x, (Box.height * x) + Box.y + Box.height, Box.width, Box.height),btns_food[x], ""))
{
selected_btn_food = btns_food[x];
editing = false;
}
}
}
// What happens when the food selection is choosen:
if (selected_btn_food == "Eat") {
slot00_value = 0;
slot00_type = 0;
slot00 = false;
selected_btn_food = "None";
}
if (selected_btn_food == "Drop") {
selected_btn_food = "None";
}
if (selected_btn_food == "Cancel") {
selected_btn_food = "None";
}
Yepp, this is all really static and bad. so if I wanted to continue on this I need to make a work around getting it more dynamic or use thousands of If statements, but I can see this is way to bad to bother with it.
At the moment I will read about Enum and loops trying to understand them. My inventory doesn’t have to be super advance, I just hope to learn and create something I understand and can Use 100%. I don’t wanna buy a asset just to take a shortcut even if it’s tempting at times as I really want a inventory system to work ![]()
After reading what I tried to created you might have ideas for me. The original thread is here if you wanna take a look.
Motivated to learn and make this work, but I’m having a hard time learning it by googling as it’s a advance task for me.


