Search Unity

Input.GetMouseButton breaks when focus is switched

Discussion in 'Scripting' started by Ranom, Feb 24, 2021.

  1. Ranom

    Ranom

    Joined:
    Oct 10, 2012
    Posts:
    49
    Super simple script:

    Code (CSharp):
    1. public class MouseTest : MonoBehaviour
    2. {
    3.     private bool lastHeld;
    4.  
    5.     public void Update()
    6.     {
    7.         var isHeld = Input.GetMouseButton(0);
    8.         if (isHeld != lastHeld)
    9.         {
    10.             Debug.Log($"Frame: {Time.frameCount}, Held: {isHeld}");
    11.             lastHeld = isHeld;
    12.         }
    13.     }
    14. }
    Steps to recreate:
    1. Run game
    2. Click (value changes to true ✅)
    3. Release -> (value changes to false ✅)
    4. Click clear in editor log below (losing focus on game)
    5. Click back in game (value changes to true, then false next frame! ❌)
     
  2. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    60
    Hi,
    I'm having the correct behaviour and can't reproduce.
    I'm running 2019.4.13f1, what's your Unity version?
     
  3. Ranom

    Ranom

    Joined:
    Oct 10, 2012
    Posts:
    49
    I'm running 2020.2.1f1 on Windows 10 Home.
     
  4. Ranom

    Ranom

    Joined:
    Oct 10, 2012
    Posts:
    49
    Just an addendum:

    Clicking outside the unity editor window does not cause the bug. (e.g. on the windows desktop)
    Clicking on other Unity editor components does (e.g. on an inspector window)