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. Dismiss Notice

What is wrong with if statement ? (C#)

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

  1. DoruKs1

    DoruKs1

    Joined:
    Jan 14, 2015
    Posts:
    28
    Hello everyone. Please, can you help me?
    Code (CSharp):
    1. if (code == true){
    2. GUI.Box (new Rect (10,10,10,10),"");
    3. if (GUI.Button(new Rect(10,5,5,5),"") ){ Debug.Log("WHATISWRONGPLSHELP")}
    4.  
    5. }
    Think that when a bool is true; first a gui box opens, then another gui button opens in that box and finally an if that checks if player clicks to that button.
    But when I do it the button just can't clickable(clickeble?) . What is wrong?
     
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    just tried it out, did that compile ok? noticed you have a missing semicolon after the debug.log.

    on line 1, does code equate to true?
    so does it show the button, as well as the background?

    ive just tried it and this works, just had to beef up the sizes a bit so i could see it with my old eyes :)

    Code (CSharp):
    1.     void OnGUI()
    2.     {
    3.         bool code = true;  // just for testing sake
    4.  
    5.         if (code == true){
    6.             // excuse my resizing, just so i could see it in my current project
    7.             GUI.Box (new Rect (10,10,110,110),"Background Box");
    8.             if(  GUI.Button(new Rect(30,30,60,60),"ClickMe!!")  )
    9.             {
    10.                 Debug.Log("Button Was Clicked!!");
    11.             }
    12.         }
    13.     }
     
    DoruKs1 likes this.
  3. DoruKs1

    DoruKs1

    Joined:
    Jan 14, 2015
    Posts:
    28
    Dude, my script is more complicated than this example script. I think that I have to put something between 2 results. What do you think?
     
  4. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    You are saying that the button displays on screen, and it just isnt clickable, does it behave as a button should? like a mouse over state etc. if it does, then there shouldnt be an issue with the button being clicked. so not sure there.

    Hard to speculate from looking at a snippit, all i can really give, with what little i understand, is some steps to try and localise any issues.

    i would start of by putting a few debug lines out to the console at logical points to ensure that parts of the script are firing when they should. so at least that way you can start narrowing down what steps work as they should.

    could possibly just check your bracings, in mono if you highlight the opening brace it shows the corresponding closing. least that way you can check your code blocks and no braces are incorrect.

    might be something as easy as a misstyped cAsE somewhere.