Hello. I just wanted to know if we can open a if statement, in a if statement! Actually, I tried but it’s not working…
You mean just an if statement inside an if statement? Or?
int i = 0;
if(i == 0)
{
i = 10;
if(i == 10)
{
Debug.Log("Hello");
}
}
Of course you can.
int a = 5;
int b = 10;
int c = 15;
if(a < b)
{
if(b < c)
{
Debug.Log("This is a nested if statement")
}
}
Sometimes it’s better to combine your if condition though, such as
if(a < b && b < c)
{
//do stuff
}
But can you give example with GUI?
What do you want to do in the GUI ?
When player clicks button a GUI box will open and blah blah…
void OnGUI() {
if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
Debug.Log("Clicked the button with an image");
if (GUI.Button(new Rect(10, 70, 50, 30), "Click"))
Debug.Log("Clicked the button with text");
}
This is snipped from the manual. For more details check out :
But what about if statement in a if statement?
hah…then please explain a bit more about what you want to do i.e the kind of checks that you
want to perform in the if…i.e explain something in this form :
if( my_first_check == true) {
if( my_second_check == true) {
}
}
Just tell me that can I do that:
void OnGUI{
if (GUI.Button(new Rect(10,10,30,30),"")){
if(GUI.Button(new Rect(10,10,30,30),""));
}
}
No, because this…
if(GUI.Button(new Rect(10,10,30,30),""));
…is not a valid branch if statement, as there is no branching.
An if has to be followed by a statement or a block { … }.
You can do that (even though there’s a little mistake), but it’s not what you’d expect. You’ll probably not even see the second button at all, because it will only be there once when you’ve clicked the button, it will not be there continuously.
That happens because OnGUI can be called several times a frame, and it will only show up during the call (or the next call, it depends on the timing) that you’ve clicked the first button.
What you probably wanna do is to toggle a boolean there which will then allow you to draw the button as long as it is true. However, there have been several threads about this causing problems as the GUI method is actually split into two calls, straight out of my head (not quite sure, correct me if i’m wrong) it was something like registering the elements first and draw them in another run.
@Suddoha is correct…Also, if you have Unity 4.6, then you can consider using
the new UI system. Its so much cooler than GUI.
I’ll be honest,I never liked that UI thing… It is always… makes me think, and it can be everywhere.