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

How to Disable GUI Button Immediately After Click

Discussion in 'Immediate Mode GUI (IMGUI)' started by darrylf, Feb 25, 2014.

  1. darrylf

    darrylf

    Joined:
    Feb 25, 2014
    Posts:
    1
    Hello guys!!
    I'm just new in Unity game development and always find every posts and threads here helpful to my project
    I'm currently working with a battle system which triggers the attack animation by the GUI.button event.
    What I want to have is how can I immediately disable the GUI button when I click it so that it will not trigger another another attack function.
    This is a Question Answer type of Battle System
    Here's my code:
    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.         ...
    5.         battleQ = GUI.Window(2, battleQ, battleWin,"Tasyo",battleSkin.window);
    6. }
    7. void battleWin (int winID)
    8. {
    9.     ...
    10.         if (slash == true)
    11.             GUI.enabled = true; // enables the GUI button to make attack
    12.         else
    13.             GUI.enabled = false;
    14.  
    15.         if(GUI.Button(AttackB,">"))
    16.         {
    17.             slash = false;
    18.             GUI.enabled = false;
    19.  
    20.         //player attack
    21.         if (atake == true)
    22.         {
    23.             bida.transform.position = new Vector3(319,10,271);
    24.             playerAnim.SetBool("idle2ToIdle1", true);
    25.         }
    26.         else
    27.         {
    28.             playerAnim.SetBool("idle2ToIdle0", true);
    29.         }...
    30.  
    What happen is, when I click the GUI.button. it does the animation, but the button is still active.
    This give a enables the Gui button to be clicked again even if the execution of animation and some functions are not done. And when it does, It jams the battle system.
    The GUI.button only disable when it's done executing the code.. if I'm not mistaken.

    Hope for anyone's help with my first issue.
    Thanks in advance :)
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Something like this should work, I use JS :

    Code (csharp):
    1.  
    2. var hasClicked : boolean = false;
    3.  
    4. function OnGUI(){
    5. if(!hasClicked  GUI.Button(Rect(0,0,100,50),"Attack")){
    6. hasClicked = true;
    7. //Do something else
    8. }
    9. }
    10.  
    After you click it, "hasClicked" becomes true so the button magically dissapears :)
     
  3. Rodrigo Cordova

    Rodrigo Cordova

    Joined:
    Mar 3, 2014
    Posts:
    1
  4. Benj4_

    Benj4_

    Joined:
    Nov 12, 2015
    Posts:
    38
    Do You Know How To Do It In C#
     
  5. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Code (CSharp):
    1. public bool hasClicked;
    2.  
    3. void OnGUI(){
    4. if(!hasClicked && GUI.Button(new Rect(0,0,100,50), "Attack")){
    5. hasClicked = true;
    6. //Do something else
    7. }
    8. }