Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

onGUI Bug?

Discussion in 'Immediate Mode GUI (IMGUI)' started by shabazzster, Jul 4, 2011.

  1. shabazzster

    shabazzster

    Joined:
    Mar 17, 2010
    Posts:
    104
    I have my buttons and my windows all laid out. Each button will open a window in the appropriate cylce.
    The bug stems from my windows getting automatically clicked at start up.
    The script
    Code (csharp):
    1. if (GUi.Button.etc)
    2. {
    3. Apllication.LoadLevel(2):
    4. ;
    5. }[/ is supposed to load a new level after i click the button
    [/CODE]

    When using GUI Skins does anyone else get buttons being automatically clicked on start up?

    If so what is the appropriate script for clicking a button and making something happen OnGUI?
    in C# please thanks
    ~K
     
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This should be just what you have:

    Code (csharp):
    1. public class MyScript : MonoBehaviour
    2.     void OnGUI() {
    3.         //  Load Level 01
    4.         if (GUI.Button(new Rect(10, 10, 150, 20), "Load Level 01"))
    5.             Application.LoadLevel ("Level 01");
    6.             // Note the different overrides for this...
    7.             // ... this could be: Application.LoadLevel (1);
    8.         }
    9.     }
    10. }
     
  3. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    just to add some tip, you can declare a var for the GUI.Button... event. so if you are declaring a condition, you only write :

    Code (csharp):
    1.  
    2. var level1Button = GUI.Button(etc..etcc.etc..);
    3.  
    4. if (level1Button){
    5.   //Could you please load my level?
    6.   }
    7.  
    when you declare the var, at the same time you are drawing the button..
     
  4. shabazzster

    shabazzster

    Joined:
    Mar 17, 2010
    Posts:
    104
    @ Little Angel ..I'm diggn your new gravatar ..its cute..Lol

    But the situation is (my buttons are pressing themselves automatically "on awake") but i need them to only depress, when i choose to press them.

    I will attempt to draw raw buttons from the basic fixed layout (without using gui skins) to see of that will solve the problem.

    In any event, i need a set of statements that will allow me to easily instruct buttons(when pressed by me) as i see fit .

    Thanks' bros
    ~K
     
    Last edited: Jul 4, 2011
  5. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    To answer your question, no, I have not had it happen on startup but it did happen to me just recently where I forgot to add a 'break' after a switch statement and THAT cased some erratic behavior.

    My point is, please copy and paste your existing code in here for us to inspect as this issue sounds like a spelling mistake somewhere. See example:
    Code (csharp):
    1.  
    2. function OnGUI()
    3. {
    4.   if (GUI.Button(Rect(0,0,10,10), "Quit the game"));
    5.      Application.Quit();
    6. }
    7.  
    Okay, okay... so I forgot the command to quit the application... ;) the point is, if you look at that code very carefully, you will see that you application will quit upon startup. Can you spot why?
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Trivial ;)
     
  7. shabazzster

    shabazzster

    Joined:
    Mar 17, 2010
    Posts:
    104
    Hey MrDude; is it because you have a semicolon at the end of your if statement?
    Should there just be a semicolon at the end of your Application.LoadLevel?
     
  8. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Correct! :)
    Simple 1 letter spelling mistakes can lead to big trouble! So yeah, show us what you do and let us look for spelling mustakes in your code :)
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Heh! And I just spotted an error in MY example code! Forgot the leading { after the if!

    Code (csharp):
    1. if (GUI.Button(new Rect(10, 10, 150, 20), "Load Level 01"))
    should be:

    Code (csharp):
    1. if (GUI.Button(new Rect(10, 10, 150, 20), "Load Level 01")) {