- Home /
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()));
}
}
Answer by Bunny83 · Apr 29, 2011 at 05:46 PM
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();
}
Bunny83 Thank for the reply it's exactly what i was looking for!!! Works greats!!!
Answer by denewbie · Apr 29, 2011 at 05:05 PM
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());
}
}
}
Like "xCR$$anonymous$$x 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.
Answer by xCRKx TyPHooN · Apr 29, 2011 at 05:12 PM
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.
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 "Inventory$$anonymous$$anager.inventory"
Answer by calyston000 · Apr 29, 2011 at 06:04 PM
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!
Your answer
Follow this Question
Related Questions
Unity Hangs/Crash when remove GUI Button. 2 Answers
Trying to get an array to make working buttons. [Script Update] 2 Answers
Making a set of buttons from an Array 1 Answer
change texture of GuiButton in for loop 1 Answer
Gathering GameObjects and then creating a button for each GameObject?? 2 Answers