- Home /
non-displaying GUILayout elements when using yield WaitForSeconds()
Hi,
I am trying to update the content of a GUILayout.Box every second. For doing the job I thought about using yield WaitForSeconds(1) and a boolean in the OnGUI(), as shown below. The problem is that it does not show the GUILayout.Box. I checked (through print()) that the function is called every second (so the WaitForSeconds(1) works perfectly) and prints (through the print()) the values I want.
So, I don't know why the GUI.Layout is not displayed...
I would apreciate very much any help
function OnGUI () {
if (!auxbool){
functionName(index);
}
function funtionName (num : int) {
GUILayout.BeginArea(Rect(800,10,80,200));
// Starts a vertical group
GUILayout.BeginVertical ("box");
GUILayout.Box (Resources.Load("image"),GUILayout.ExpandWidth(false) );
GUILayout.Box ( ""+aux[num],foGuiStyle);
GUILayout.EndVertical();
GUILayout.EndArea();
auxbool = true;
yield WaitForSeconds(1);
auxbool = false;
}
Answer by Mike 3 · Mar 21, 2011 at 01:36 PM
Unity's GUI system (OnGUI + GUI. + GUILayout. etc) work in immediate mode - you have to call your gui items every frame you want them on the screen
Right now you're only drawing them once every second, which has the effect of looking like it's not drawing them at all
I can't see why you're doing it in this instance, but if you need to do heavy calculations, do that part alone once a second, leave the gui rendering as it is
To change your value once a second:
var str = "Test"; var strToShow = "";
function Start() { InvokeRepeating("UpdateValue", 1, 1); }
function UpdateValue() { strToShow = str; }
Thanks $$anonymous$$ike, you are totally right, I don't know what I was thinking about.
$$anonymous$$y objective is to change the value it is shown in the GUILayout.Box every second, for example. So for 1 second it must show the same value, and then change for the new value, wait for another second, and again change and wait...
so i need to implement a trigger... somehow... every sencond
Your answer
Follow this Question
Related Questions
Function On GUI 2 Answers
yield WaitForSeconds in OnGUI 4 Answers
GUILayout, is there a way to bunch horizontal elements together? 1 Answer