OnGUI() drawin wont work in if()

Hi folks,
i got a interesting problem.

OnGUI(){
if(buttonPressed)
DrawName();
}

void DrawName(){
GUI.Label(new Rect(100,100, 300, 30), name);
}

That wont work… but if i write the GUI.Label into the OnGUI() method, without the if() it work…
exactly the same if i wrote just the method into the OnGUI() method, without the if()…

buggy? or special behavior of OnGUI() ?

It’s not very clear what you’re trying to achieve.

First, why are you storing the button press state in a variable rather than finding out from a GUI.Button() call?

Second, why are you not using indentation and scope markers?

You OnGUI() code is actually this:

if(buttonPressed)
{
  DrawName();
}

i want to achieve that after pressing the button the label would be drawn.

The Button is gettin drawn in a Method and there i set the presse variable.

i indent my code, but here i wrote it by hand and u dont need scope markers if you just write one instruction after the if.

Works:

OnGUI(){
DrawName();
}

void DrawName(){
GUI.Label(new Rect(100,100, 300, 30), name);
}

Dont work:

OnGUI(){
if(buttonPressed)
DrawName();
}

void DrawName(){
GUI.Label(new Rect(100,100, 300, 30), name);
}

and also

OnGUI(){
if(buttonPressed)
GUI.Label(new Rect(100,100, 300, 30), name);
}

wont work

everytime i wrote in OnGUI() a if, within a drawin instruction it wont work… just with the if()

PS: buttonPressed is true :wink:

My guess is that the bool turns false in your other code.

im in the DrawName() method,
but the GUI.Label call wont work…

i got a workaround… but if someone know sth let me here

Is what you’re posting, the whole script? Where is buttonPressed set? More importantly, where is it set to true? If buttonPressed is false, that is exactly what should be happening.

the button would be drawn in a method called
DrawButton which is implemented into OnGUI().

I dont know whats so important about all the things around :roll:

i get into the DrawMethod, i get the true value. but the line with the GUI.Label wouldnt be executed…

Well, maybe, but right now buttonPressed is not true. Therefore the if statement does not execute.