Making an Inventory (Basics)

Whoa, the examples I see for inventories around here are very confusing! I was reading through the Unity reference site about Gui basics, but got lost on grids. I understand making a Gui box, and making grids in general, but how do I make PAGES of different grids for one gui (inventory)?

Basically, here are my goals-

  1. have a small box to display the current inventory Gui when clicked (a bag icon button)

  2. if you click this, a new page pops up showing a box with a grid in it(or set up in a grid like fashion) for the inventory

  3. if a player finds/nears an item (assigned with a specific texture) and presses a button, the grid squares are assigned the items texture

  4. the item in the scene is destroyed

  5. if you click the texture(item) in the inventory, you instantiate that item, and the texture in the inventory dissapears.

Also, in general, how do you go about putting a number on a button that starts at zero and is added/subtracted to when you do collect/withdraw the same thing multiple times?

And how do you toggle a gui's visibility? I know you can use enable=false; but wouldn't that restart the whole gui each time you close it?

When I did try out the grid example in the reference site, the buttons got stressed and smaller the bigger I made the string.. Why? Here it is again

var selectionGridInt : int = 0;
var selectionStrings : String[] = ["Grid 1", "Grid 2", "Grid 3", "Grid 4"];

function OnGUI () {
    selectionGridInt = GUI.SelectionGrid (Rect (25, 25, 100, 30), selectionGridInt, selectionStrings, 2);


Also, how come the first grid button is automatic? I tried this for pressing grid buttons

if(selectionGridInt == 0) {
//do something;

but 0 doesn't wait until I push it!

I know I'm asking a lot of questions, but the examples I find are giving me headaches. Any help is very much appreciated!

I was thinking about deleting this whole question, but I will leave it with the answers I found for anyone else who might need this info.

For pages in an inventory:

 enum Page {
        None,Open,Page1,Page2,Page3,Page4//etc however many pages you need

    private var currentPage:Page;

    function Awake(){


    function OnGUI(){

    switch (currentPage) {

                case Page.Open: Open(); break;
                case Page.Page1: Page1(); break;
                //case Page.Page2: Page2(); break;
                //case Page.Page3: Page3(); break;
                //case Page.Page4: Page4(); break;


    function Open() {

    if (GUI.Button(Rect (20,40,80,20), "Iventory")) {
            currentPage = Page.Page1;


function Page1() {

    if (GUI.Button(Rect (20,40,80,20), "Close")) {
            currentPage = Page.Open;


This is basically a summary for the guide I found here link text

As for a grid with icon buttons, I found this very useful guide and example here


I've done something very similar few weeks ago, and I think I can give you some advices.

First, I read somewhere that buttons are way too expensive for that situation, where you can end up with 100 squares or so. So I just used labels and a simple code that tell me which square I am over (and maybe hilight it) :

int x = mousePos.x / squareWidth;
int y = mousePos.y / squareHeight;
// Might be a good idea to clamp those, btw

Then I have an array of size nbLine * nbColumn, so each square know what item it contain

Finally, when there is a clic, you can access the item with something like that

item = itemArray[ x + y*nbColumn ];
item.drop(); // or whatever

So, you can easily modify the number of squares in your inventory, the size etc. When you pick up something, you need to scan the itemArray to check which square is empty to add a new item. I think you've got anything you need with that.

About Gui visibility, enabled = false will disable the component, not the GameObject. That means each time you use enabled, the functions called are OnEnable and OnDisable, which you can override btw (is Start() called when you do = true ? I'm not sure ...). So it won't probably restart anything.

Hope that helps.