Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can we do this?

Discussion in 'Scripting' started by DoruKs1, Feb 17, 2015.

  1. DoruKs1

    DoruKs1

    Joined:
    Jan 14, 2015
    Posts:
    28
    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...
     
  2. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    You mean just an if statement inside an if statement? Or?

    Code (CSharp):
    1. int i = 0;
    2. if(i == 0)
    3. {
    4.    i = 10;
    5.    if(i == 10)
    6.    {
    7.        Debug.Log("Hello");
    8.    }
    9. }
    10.  
     
  3. matt123miller

    matt123miller

    Joined:
    Oct 24, 2014
    Posts:
    5
    Of course you can.
    Code (csharp):
    1.  
    2. int a = 5;
    3. int b = 10;
    4. int c = 15;
    5.  
    6. if(a < b)
    7. {
    8. if(b < c)
    9. {
    10. Debug.Log("This is a nested if statement")
    11. }
    12. }
    13.  
    Sometimes it's better to combine your if condition though, such as
    Code (csharp):
    1.  
    2. if(a < b && b < c)
    3. {
    4. //do stuff
    5. }
    6.  
     
  4. DoruKs1

    DoruKs1

    Joined:
    Jan 14, 2015
    Posts:
    28
    But can you give example with GUI?
     
  5. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    What do you want to do in the GUI ?
     
  6. DoruKs1

    DoruKs1

    Joined:
    Jan 14, 2015
    Posts:
    28
    When player clicks button a GUI box will open and blah blah...
     
  7. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Code (CSharp):
    1.  void OnGUI() {
    2.      
    3.         if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
    4.             Debug.Log("Clicked the button with an image");
    5.        
    6.         if (GUI.Button(new Rect(10, 70, 50, 30), "Click"))
    7.             Debug.Log("Clicked the button with text");
    8.        
    9.     }
    This is snipped from the manual. For more details check out :
    http://docs.unity3d.com/ScriptReference/GUI.Button.html
     
  8. DoruKs1

    DoruKs1

    Joined:
    Jan 14, 2015
    Posts:
    28
    But what about if statement in a if statement?
     
  9. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    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 :

    Code (CSharp):
    1. if( my_first_check == true) {
    2. if( my_second_check == true) {
    3.  
    4. }
    5. }
     
  10. DoruKs1

    DoruKs1

    Joined:
    Jan 14, 2015
    Posts:
    28
    Just tell me that can I do that:
    Code (CSharp):
    1.  void OnGUI{
    2. if (GUI.Button(new Rect(10,10,30,30),"")){
    3.     if(GUI.Button(new Rect(10,10,30,30),""));
    4. }
    5. }
     
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,516
    No, because this...
    Code (csharp):
    1. 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 { ... }.
     
  12. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
    gamer_boy_81 and angrypenguin like this.
  13. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    @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.
     
  14. DoruKs1

    DoruKs1

    Joined:
    Jan 14, 2015
    Posts:
    28
    I'll be honest,I never liked that UI thing... It is always... makes me think, and it can be everywhere.