Search Unity

  1. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  2. Unity 2017.1 is now released.
    Dismiss Notice
  3. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  4. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

How to Disable GUI Button Immediately After Click

Discussion in 'Extensions & OnGUI' 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. Intense_Gamer94

    Intense_Gamer94

    Joined:
    Jun 20, 2013
    Posts:
    1,064
    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. benjamin7006

    benjamin7006

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

    Intense_Gamer94

    Joined:
    Jun 20, 2013
    Posts:
    1,064
    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. }