- Home /
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;
}
This is inside of an OnGUI function, right? Can you post the whole function? Are there any warnings/errors in the console?
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
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?
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;
}
}
}
"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?
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]);
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.
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.
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.
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.
Your answer
Follow this Question
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