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 Kag359six · Mar 24, 2012 at 05:37 PM · onguifor-loopaddframeeach

Looping inside OnGUI

I'm trying to use a for loop to create a button for every item is found inside the list menuItems. However i also want each button to be 50 pixels away from the last button. To do this i add 50 pixels to the xPos of the button after each loop. the problem is that it keeps adding 50 to it every frame. Help?

 var rayLength : float = 30;
 
 private var buttonPosX : int = 10;
 
 private var buttonPosY : int = 10;
 
 var menuItems : Array = [];
 
 var itemTextures  : Array = [];
 
 private var currentGameObject : String;
 
 function Update () {
 
 InventoryRay();
 //PrintList(menuItems);
 
 }
 
 function PrintList(list) {
 
 Debug.Log(list);
 
 }
 
 function InventoryRay() {
 
 var hit : RaycastHit;
 
 Debug.DrawRay(transform.position, transform.forward * rayLength, Color.red);
 
 if(Physics.Raycast(transform.position, transform.forward, hit, rayLength))
     {
         if(Input.GetMouseButtonDown(0))
         {
             if(hit.collider.gameObject.tag == "collectable")
             {
                 Debug.Log("collectable");
                 currentGameObject = hit.collider.gameObject.name;
                 Debug.Log(currentGameObject);
                 menuItems.Add(currentGameObject);
             }
         }
     }
 
 
 }
 
 function OnGUI() {
 
 GUI.Box(Rect(0, 0, Screen.width/2, Screen.height/2), "Inventory");
 for(items in menuItems)
 {
     if(GUI.Button(Rect(buttonPosX, buttonPosY, 100, 100), items))
     {
         
     }
     buttonPosX = buttonPosX + 50;
 }
 
 }
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

2 Replies

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

Answer by AlucardJay · Mar 24, 2012 at 05:47 PM

You need to reset buttonPosX at the start of each OnGUI frame loop.

For button spacing, you want to add the depth of the button with the space you want in-between. (I made the button depth 50)

Try this to replace your current GUI function :

 function OnGUI() {
     GUI.Box(Rect(0, 0, Screen.width/2, Screen.height/2), "Inventory");
     buttonPosX = 10; // reset buttonPosX everytime OnGUI is called
     for(items in menuItems)
     {
         if(GUI.Button(Rect(buttonPosX, buttonPosY, 100, 50), items))
         {
             // do stuff
         }
     buttonPosX = buttonPosX + 100; // button depth (50) PLUS space between buttons (50)
     }
 }
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
1

Answer by Eric5h5 · Mar 24, 2012 at 05:48 PM

You're not resetting buttonPosX before the loop in OnGUI, so it will just keep adding to it forever. It would be better and simpler to use a standard for loop (`for (var i = 0; i < menuItems.Length; i++)`) instead of a for/in loop, then you can just use the loop index instead of making a global variable. Also, don't use Array, use List. http://unifycommunity.com/wiki/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F

Comment
Add comment · Show 4 · 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 AlucardJay · Mar 24, 2012 at 05:54 PM 0
Share

thx for the link , very useful info there :)

avatar image Kag359six · Mar 24, 2012 at 06:48 PM 0
Share

thanks! although alucardj's solution works too.

avatar image Kag359six · Mar 24, 2012 at 06:55 PM 0
Share

im having an issue with the standard for loop. when i use it, all of a sudden its says that the there is no valid arguement for the gui.button line

avatar image AlucardJay · Mar 24, 2012 at 07:35 PM 0
Share

try debugging to see where it's breaking.

 for (var i = 0; i < menuItems.Length; i++)
 {
     print ("i "+i+" : menuItems["+i+"] = "+menuItems[i]);
     // other code
 }

just guessing, you may be running out of places in the menuItems array.

see the .length may be 12 , but counting from 0 , you get to 11.

to fix this :

 for (var i = 0; i < (menuItems.Length - 1); i++)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

do for and while loops happen all in one frame? 1 Answer

ExecuteInEditMode is not working. 1 Answer

Why my button in for loop isn't working? 1 Answer

Instantiate From array, but cycle trough each gameobject 1 Answer

[C#] Programm does not add to list 1 Answer


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