x


Creating GUI Buttons with a for loop from an array

Hi all! Here i go! I got a inventory array where i put my item name in it once they've been picked up! Then i want to create a button for each items listed in this array with a for each loops.

Problem is the loop is working but it put all the content of the inventory in one button! How can i create a button for each item. I've also tried with a regular for loop. So anyway here my code :

function OnGUI ()
{
    //Show the content of the inventory and convert it to string
    for(i in InventoryManager.inventory)
    {
        if(GUI.Button (Rect (25, 25, 150, 30), InventoryManager.inventory.ToString()));
    }
}
more ▼

asked Apr 29 '11 at 04:58 PM

calyston000 gravatar image

calyston000
3 3 5 9

(comments are locked)
10|3000 characters needed characters left

6 answers: sort voted first

You just gave us a small piece of your code. We don't know what's inside your inventory enumeration. I just guess it's a string. In that case it should look like that:

function OnGUI ()
{
    var yOffset = 0.0;
    for(i in InventoryManager.inventory)
    {
        if( GUI.Button (Rect (25, 25+ yOffset, 150, 30), i) )
        {
            Debug,Log("pressed Item " + i);
        }
        yOffset += 35:
    }
}

For such loop-created buttons it's much easier to use GUILayout:

function OnGUI ()
{
    GUILayout.BeginArea(Rect (25,25,150,300));
    GUILayout.BeginVertical();
    for(i in InventoryManager.inventory)
    {
        if( GUILayout.Button(i) )
        {
            Debug,Log("pressed Item " + i);
        }
    }
    GUILayout.EndVertical();
    GUILayout.EndArea();
}
more ▼

answered Apr 29 '11 at 05:46 PM

Bunny83 gravatar image

Bunny83
46.8k 12 50 210

Bunny83 Thank for the reply it's exactly what i was looking for!!! Works greats!!!

Apr 29 '11 at 06:11 PM calyston000
(comments are locked)
10|3000 characters needed characters left
function OnGUI ()
{
    //Show the content of the inventory and convert it to string
    for(i in InventoryManager.inventory)
    {
        if(GUI.Button (Rect (25 + (i * 150), 25, 150, 30), InventoryManager.inventory.ToString()))
        {        
               // Place your function
               print("You have selected " + InventoryManager.inventory.ToString());
        }
    }
}
more ▼

answered Apr 29 '11 at 05:05 PM

denewbie gravatar image

denewbie
732 3 3 17

Like "xCRKx TyPHooN" you're showing the same content on every button and i guess what ever is inside the inventory array/enumeration is not an integer that can be used as offset.

Apr 29 '11 at 05:36 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

The GUI Button will be spawning on top of one another, you should increment one of the rect variables to move the box as you place your inventory. Example:

var yOffset = 0.0f;
foreach(Inventory i in InventoryManager.inventory)
{
    if(GUI.Button (Rect (25, (25 + yOffset), 150, 30), InventoryManager.inventory.ToString()))
    yOffset += 30;
}

This will stack the gui boxes on top of one another.

more ▼

answered Apr 29 '11 at 05:12 PM

xCRKx TyPHooN gravatar image

xCRKx TyPHooN
287 3 5 14

Well, your example is written in C# the OP uses JS. The offset thing is right but you place them offscreen. I think you want to use "+= 30". Besides that inventory seems to be an enumeration so the button context should be "i" and not "InventoryManager.inventory"

Apr 29 '11 at 05:34 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

All your buttons have the same position ?

more ▼

answered Apr 29 '11 at 05:07 PM

Jeston gravatar image

Jeston
419 27 31 34

(comments are locked)
10|3000 characters needed characters left

I've tried both codes and it's doing the same thing both! It's creating only one button with all the inventory in it! Also just for the record, the array inventory is an array that i've declared just like that : inventory = new Array (); //declaring the inventory

What i'm not understanding both of the code up there should works and I tougth the same that it was the offset the problem!! But it's it's creating only one instance of the buttons!

By the thanks ya all for the help!

I'm giving you all the sample of the code here :

InventoryManager

static var inventory : Array; //Starting the inventory
var GuiPrefab : GameObject;
var guiState : boolean = false;

function Awake ()
{
    inventory = new Array (); //declaring the inventory
}
function Update ()
{
    CheckInventory();
}

////////////////////////////////////Additionnal Function/////////////////////////////////////
//Function to show the inventory and what in it!
function CheckInventory()
{
    if(Input.GetKeyDown("i") && guiState==false)
    {
        var GuiInventory = Instantiate(GuiPrefab,transform.position,transform.rotation);
        guiState=true;
    }
    else if(Input.GetKeyDown("i") && guiState==true)
    {
        Destroy(GameObject.FindWithTag("GUI"));
        guiState=false;
    }
}

Pickup Script

var initialColor : Color; //initailise the variable for the initale color for the mouse over.

function Awake ()
{
    initialColor =  gameObject.renderer.material.color;//initailise the variable for the initale color for the mouse over.
}

function OnMouseUp ()
{
    if(gameObject.FindGameObjectsWithTag("Item")) //if the object is an item you can pickit up
    {
        print("You've picked the object");
        Destroy(gameObject); // Get rid of the object
        AddToInventory(); //call the function to put item in inventory
    }
}
function AddToInventory()
{
    print("adding this object to the inventory: " + gameObject.name); //tell you what happened
    InventoryManager.inventory.Push(gameObject.name); //get acces to the inventory array from InventoryManager and put it in it!
}

function OnMouseOver ()
{
    if(gameObject.FindGameObjectsWithTag("Item"))
    {
        print("This is an item");
        gameObject.renderer.material.color = Color.red; //higligth the object so we know you can pickit up
    }
}

function OnMouseExit ()
{
    if(gameObject.FindGameObjectsWithTag("Item"))
    {
        print("Getting out of item");
        gameObject.renderer.material.color = initialColor; //on the mouse exit return it to it inital texture;
    }
}

GuiInventory

function OnGUI()
{
    var yOffset = 0.0f;
    for(i in InventoryManager.inventory)
    {
        if(GUI.Button (Rect (25, (25 + yOffset), 150, 30), InventoryManager.inventory.ToString()))
        yOffset += 30;
    }
}

Hope it can help ya!

more ▼

answered Apr 29 '11 at 06:04 PM

calyston000 gravatar image

calyston000
3 3 5 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3811
x1396
x819
x304

asked: Apr 29 '11 at 04:58 PM

Seen: 2916 times

Last Updated: Apr 29 '11 at 04:58 PM