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 pvzkiller · Oct 22, 2012 at 10:44 PM · guibuttonlooponguiforeach

OnGUI button created by a foreach loop

Hi all, it seems like I cannot get the buttons to even display. I don't understand why.... maybe because we cannot create GUI buttons inside of a foreach loop? I don't see why this could be impossible...

 foreach(string jjj in maps)
             {
             Debug.Log ("------------------- OK: " + jjj);
             //I correctly see all the jjj strings in the console, one per line. The problem is below.
                     
                 if(GUI.Button(new Rect(10,60 + yOffset,50,25), jjj)) 
                 {    
                     loadlevelname = jjj;
                     Debug.Log ------------------LOADLEVELNAME: " + loadlevelname);
                 }
                 yOffset += 35;
                     
             }
Comment
Add comment · Show 3
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 DaveA · Oct 22, 2012 at 10:46 PM 0
Share

This is inside of an OnGUI function, right? Can you post the whole function? Are there any warnings/errors in the console?

avatar image pvzkiller · Oct 22, 2012 at 10:54 PM 0
Share

Yes it is inside of an OnGUI function. No errors in the console. Simply notthing get displayed.

Here is my latest code: http://pastebin.com/Xz7Tdjh2

avatar image DaveA · Oct 23, 2012 at 12:32 AM 0
Share

I did not see this code fragment in the pastebin code. Can you edit your question and paste (using the 010/101 button) the routine here? I think there's probably a problem in you you set up 'maps', you say it's a string split right?

4 Replies

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

Answer by Xroft666 · Oct 22, 2012 at 11:42 PM

Man, it works fine. Just check if your "maps" is full of content.

Here you are

 using UnityEngine;
 using System.Collections.Generic;
 
 public class SomeScript : MonoBehaviour
 {
     List<string> maps = new List<string>();
     
     void Start()
     {
         maps.Add("0");
         maps.Add("1");
         maps.Add("2");
         maps.Add("3");
         maps.Add("4");
         maps.Add("5");
     }
     
     void OnGUI()
     {
         float yOffset = 0f;
         
             foreach(string i in maps)
            {
                 if( GUI.Button (new Rect (25, 25+ yOffset, 150, 30), i) )
                 {
                         Debug.Log("pressed Item " + i);
                 }
             
                 yOffset += 35;
             }
         
     }
 }
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 pvzkiller · Oct 23, 2012 at 12:13 AM 0
Share

"maps" refers to this string (before splitting): ;roger;maya;max;noel;jean;renault;sylvie And no, I confirm that no "maps" is empty. I verified this with the console. In fact it had an empty one in the first position (0), but I put: maps[0] = "empty"; just before starting the foreach, so now there is a string. But the buttons still not display, which is very strange... Any idea?

avatar image
0

Answer by DaveA · Oct 23, 2012 at 12:34 AM

I'm guessing based on comment in other answer:

"maps" refers to this string (before splitting): ;roger;maya;max;noel;jean;renault;sylvie

so if you code looks something like this it should work:

 string[] maps;
 
 maps = "roger;maya;max;noel;jean;renault;sylvie".Split(";"[0]);
 
 or
 
 s = "roger;maya;max;noel;jean;renault;sylvie";
 maps = s.Split(";"[0]);
Comment
Add comment · Show 2 · 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 pvzkiller · Oct 23, 2012 at 12:54 AM 0
Share

Thanks for caring Dave, however the problem is not splitting the string in smaller chunks; this part is working like a charm. The problem is that I cannot get the buttons to display in the OnGUI, even with a string hard-coded as the string in the button code.

avatar image pvzkiller · Oct 23, 2012 at 01:04 AM 0
Share

Oh ok I got it! The problem was, I was trying to start a new OnGUI button inside another one, and this is not working of course! Ins$$anonymous$$d I set a bool variable to true when I click on it, THEN out of this I check if the variable is either true or false and do the desired actions from there. Xroff666's solution was right and helped me to figure out. $$anonymous$$y code was different, though, and he didn't pointed to the fact that I nested a button inside another one, which was the problem here. Anyway, I accept this solution since it was right and put me on the right track.

avatar image
0

Answer by JakeLeB · Mar 25, 2015 at 01:02 PM

I know this is old but for anyone still looking for a similar answer, it's because of this:

 foreach (PhotonPlayer player in PhotonNetwork.playerList) {
             GUI.Label(new Rect(400, 15+ yOffset, 200, 25), player.name);
             yOffset += 30F;
         }

This is my code at the moment, if you put this inside a keypress you'll notice that yOffset +=30F is scrolling ALL items, not adding to the height so they are under each other. I haven't yet figured out a way to do this properly.

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 JakeLeB · Mar 25, 2015 at 01:02 PM

I know this is old but for anyone still looking for a similar answer, it's because of this:

 foreach (PhotonPlayer player in PhotonNetwork.playerList) {
             GUI.Label(new Rect(400, 15+ yOffset, 200, 25), player.name);
             yOffset += 30F;
         }

This is my code at the moment, if you put this inside a keypress you'll notice that yOffset +=30F is scrolling ALL items, not adding to the height so they are under each other. I haven't yet figured out a way to do this properly.

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

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating GUI Buttons with a for loop from an array 6 Answers

GUI.Label inside a foreach loop? 0 Answers

Menu button needs to be double clicked. 4 Answers

Not able to make a foreach loop 2 Answers

Debug.Log - Pressing Button readings. 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