- Home /
Problem is not reproducible or outdated
GUI wont show in for loop
In my game, Im allowing the player to input some numbers. I then want to display the numbers 1 - 10 on the screen and if any of the numbers the player chose are within 1-10 then change how they look. They player can continuously do this, each time the new set of numbers is displayed under the last. Im also trying to put it inside a scrollable gui so the player can navigate their matches. Here is my code thus far:
void OnGUI () {
GUI.skin = skin;
noString = GUI.TextField(new Rect (10, 65, 250, 20), noString);
if (GUI.Button (new Rect (Screen.width - 110, 65, 100, 20), "Add Game")) {
givenNumbers = noString.Split(new char[] {' '});
adding = true;
}
scrollPos = GUI.BeginScrollView(new Rect(10,110, Screen.width - 20, Screen.height - 120), scrollPos, new Rect(0, 0, 2900, 2900), true, true);
if (adding == true) {
Debug.Log("adding new game");
for(int i = 0; i < givenNumbers.Length; i++) {
foreach (int n in Enumerable.Range (1, 40)) {
if (Int32.Parse(givenNumbers[i]) != n) {
height += 32;
drawNumberUnsel(width, height, n);
}
else {
height += 32;
drawNumberSel(width, height, n);
}
}
Debug.Log(givenNumbers[i]);
}
adding = false;
}
GUI.EndScrollView();
}
public void drawNumberUnsel(float w, float h, int n) {
Debug.Log ("in drawnumbers unsel");
GUI.Box (new Rect (w, h, 30, 30), n.ToString(), GUI.skin.GetStyle ("unsel"));
}
public void drawNumberSel(float w, float h, int n) {
Debug.Log ("in drawnumbers sel");
GUI.Box (new Rect (w, h, 30, 30), n.ToString(), GUI.skin.GetStyle ("sel"));
}
for some reason, the gui wont show on the screen.. at all. How can I fix this?
Any reason you're not using the new UI system? It's much less likely to cause you problems and is actually designed to be used in games. The old OnGUI system was more for quick and dirty prototyping or displaying debug info.
Answer by maccabbe · Apr 06, 2015 at 04:35 AM
On line 33 you set adding to false so the gui will only be displayed for one frame. You should remove line 33. You also don't reset height so things will be drawn further down each frame, after you remove line 33 you should replace it with height=0; I would also note that displaying multiple things is exactly what the legacy gui system is bad at so your program will have low performance. Here is some code that works if you input a 1 and accept.
using UnityEngine;
using System;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
string[] givenNumbers;
bool adding;
Vector2 scrollPos;
int height=30;
int width=30;
string noString="";
void OnGUI() {
noString=GUI.TextField(new Rect(10, 65, 250, 20), noString);
if(GUI.Button(new Rect(Screen.width-110, 65, 100, 20), "Add Game")) {
givenNumbers=noString.Split(new char[] { ' ' });
adding=true;
}
scrollPos=GUI.BeginScrollView(new Rect(10, 110, Screen.width-20, Screen.height-120), scrollPos, new Rect(0, 0, 2900, 2900), true, true);
if(adding) {
height=0;
for(int i=0; i<givenNumbers.Length; i++) {
for(int n=1; n<=40; n++) {
int value;
if(Int32.TryParse(givenNumbers[i], out value)) {
if(value!=n) {
Debug.Log(width+" "+height+" "+n);
height+=32;
drawNumberUnsel(width, height, n);
}
else {
height+=32;
drawNumberSel(width, height, n);
}
}
}
Debug.Log(givenNumbers[i]);
}
}
GUI.EndScrollView();
}
public void drawNumberUnsel(float w, float h, int n) {
Debug.Log("in drawnumbers unsel");
GUI.Box(new Rect(w, h, 30, 30), n.ToString());
}
public void drawNumberSel(float w, float h, int n) {
Debug.Log("in drawnumbers sel");
GUI.Box(new Rect(w, h, 30, 30), n.ToString());
}
}
removing the 'adding' bool kind of works but then I cant add more games to it.. how else can i call and add GUI inside a scroll window whenever the user wants without it disappearing?