Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by calyston000 · Apr 29, 2011 at 04:58 PM · guibuttonarrayloop

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()));
    }
}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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();
}
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image calyston000 · Apr 29, 2011 at 06:11 PM 0
Share

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

avatar image
1

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());
        }
    }
}
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 29, 2011 at 05:36 PM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 29, 2011 at 05:34 PM 0
Share

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"

avatar image
0

Answer by Jeston · Apr 29, 2011 at 05:07 PM

All your buttons have the same position ?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges