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

Help with enabling and disabling mouse input

Discussion in 'Scripting' started by Kardino, Jan 21, 2021.

  1. Kardino

    Kardino

    Joined:
    Oct 8, 2020
    Posts:
    8
    Hello, I am making a quiz game and I made it so that score is added every time you click on the right answer but I have found out that you can click multiple times before the time between questions run out and get more points that you are supposed to.

    I was thinking of disabling the mouse input when the player clicks on a button and enable it when scores are added or subtracted. I have no idea how to disable and enable mouse input though can someone help me with this?
     
  2. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    You can disable your mouse with the following:
    Code (csharp):
    1. Cursor.lockState = CursorLockMode.Locked;
    2. Cursor.visible = false;
    but i would disable the button (not the mouse) if the answer was correct:

    Code (csharp):
    1.  answerButton.interactable = false;
     
    Lethn likes this.
  3. Kardino

    Kardino

    Joined:
    Oct 8, 2020
    Posts:
    8
    Thank you for the reply, there are multiple answer buttons I don't know how to reference them all.
     
  4. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    Does a button know if it is an answer button ? If yes, put a reference of that button into the script which is attached to the button and set the button via inspector, then disable it, after it was clicked. So each (answer) button handles itself.
     
  5. Havyx

    Havyx

    Joined:
    Oct 20, 2020
    Posts:
    140
    Code (CSharp):
    1.  
    2.  
    3. public Button[] answerButtons;
    4.  
    5. private void Start()
    6. {
    7.     int currentButtonID = 0;
    8.  
    9.     foreach (Button answerButton in answerButtons)
    10.     {
    11.         answerButton.onClick.AddListener(DoSomethingAfterPlayerHasPressedButton(currentButtonID));
    12.         currentButtonID++;
    13.     }
    14. }
    15.  
    16. private void DoSomethingAfterPlayerHasPressedButton(int answerButtonID)
    17. {
    18.     AnswerButtonToggleInteractable(false);
    19.  
    20.     // calculate score/do something?
    21.  
    22.     if (answerButtonID == 4)
    23.     {
    24.         CorrectAnswer();
    25.     }
    26.     else
    27.     {
    28.         IncorrectAnswer();
    29.     }
    30.  
    31.     AnswerButtonToggleInteractable(true);
    32. }
    33.  
    34. private void AnswerButtonToggleInteractable(bool value)
    35. {
    36.     foreach (Button answerButton in answerButtons)
    37.     {
    38.         answerButton.interactable = value;
    39.     }
    40. }
    41.  
    42.  
     
    Joe-Censored likes this.