Search Unity

Bug Cannot query value of control '/VirtualMouse/position'

Discussion in 'Input System' started by unity_TRCYPJlRCIbBjQ, May 22, 2021.

  1. unity_TRCYPJlRCIbBjQ

    unity_TRCYPJlRCIbBjQ

    Joined:
    Apr 29, 2019
    Posts:
    1
    Hello,

    I'm using version 1.0.2 - January 21, 2021 and I downloaded the sample "Gamepad mouse cursor", everything works great, but when I set gameObject.setActive(false) and set gameObject.setActive(true) , there is a error:

    InvalidOperationException: Cannot query value of control '/VirtualMouse/position' before 'VirtualMouse' has been added to system!

    UnityEngine.InputSystem.InputControl.ResolveDeviceIndex () (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/Controls/InputControl.cs:904)
    UnityEngine.InputSystem.InputControl.get_currentStatePtr () (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/Controls/InputControl.cs:787)
    UnityEngine.InputSystem.InputControl`1[TValue].ReadValue () (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/Controls/InputControl.cs:939)
    UnityEngine.InputSystem.UI.VirtualMouseInput.TryEnableHardwareCursor () (at Assets/Samples/Input System/1.0.2/Gamepad Mouse Cursor/VirtualMouseInput.cs:393)
    UnityEngine.InputSystem.UI.VirtualMouseInput.OnEnable () (at Assets/Samples/Input System/1.0.2/Gamepad Mouse Cursor/VirtualMouseInput.cs:284)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr, Boolean&)

    upload_2021-5-22_11-2-1.png

    How can I disable the virtual cursor and enable it without error? I want to use the system mouse again.
     
    Tymianek likes this.
  2. LeandroExHuMeD

    LeandroExHuMeD

    Joined:
    Apr 6, 2016
    Posts:
    5
    I'm having this same problem, did you solve it? :/
     
    Tymianek likes this.
  3. dmytro_at_unity

    dmytro_at_unity

    Unity Technologies

    Joined:
    Feb 12, 2021
    Posts:
    212
    Hey sounds like a real bug, would you mind to please report it to our bugtracker via Help->Report a bug? Thanks a lot!
     
  4. PaulNK

    PaulNK

    Joined:
    Apr 8, 2015
    Posts:
    27
    I just had the same issue and thought I’d post some notes. The issue in my case happens when loading a new scene which contains VirtualMouseInput. I can currently work around this by disabling the VirtualMouseInput component and then enabling it from another components Start() method.
     
    imaginationrabbit likes this.
  5. imaginationrabbit

    imaginationrabbit

    Joined:
    Sep 23, 2013
    Posts:
    349
    Using 2019.4.20f1 LTS and I am getting this bug.

    I tried disabling the component before its game object was deactivated and then enabling it after its game object was activated like the previous post describes but still getting the error.

    UPDATE: Fixed it

    To fix it go to OnEnable in the VirtualMouseInput script

    Find this bit of code

    Code (CSharp):
    1.    // Hijack system mouse, if enabled.
    2.             if (m_CursorMode == CursorMode.HardwareCursorIfAvailable)
    3.                 TryEnableHardwareCursor();
    By default its above this bit of code

    Code (CSharp):
    1.  // Add mouse device.
    2.             if (m_VirtualMouse == null)
    3.             {
    4.                 m_VirtualMouse = (Mouse)InputSystem.AddDevice("VirtualMouse");
    5.             }        
    6.             else if (!m_VirtualMouse.added)
    7.             {
    8.              
    9.                 InputSystem.AddDevice(m_VirtualMouse);
    10.             }
    You want it to be after the above code so move move it beneath it so it looks like this

    Code (CSharp):
    1.     // Add mouse device.
    2.             if (m_VirtualMouse == null)
    3.             {
    4.                 m_VirtualMouse = (Mouse)InputSystem.AddDevice("VirtualMouse");
    5.             }        
    6.             else if (!m_VirtualMouse.added)
    7.             {
    8.              
    9.                 InputSystem.AddDevice(m_VirtualMouse);
    10.             }
    11.        
    12.             // Hijack system mouse, if enabled.
    13.             if (m_CursorMode == CursorMode.HardwareCursorIfAvailable)
    14.                 TryEnableHardwareCursor();
    15.  
    You can now deactivate/activate the gameObject the VirtualMouse is on and it will work without an error popping up.
     
    Last edited: Sep 15, 2021
    Tymianek and Tornar like this.
  6. Pnvanol

    Pnvanol

    Joined:
    Jan 11, 2016
    Posts:
    116

    When virtual mouse is spawned, controller mode switches to keyboard and mouse even when using the gamepad, do you know any fix? playerinput.currentcontrolscheme switches to keyboard andmouse
     
  7. imaginationrabbit

    imaginationrabbit

    Joined:
    Sep 23, 2013
    Posts:
    349
    Are you using the example that uses VirtualMouseInput.cs?

    Is the gamepad detected? If you use

    Code (CSharp):
    1. Debug.Log(Gamepad.all.Count + " gamepad's detected");
    Is more than one detected?
     
  8. Pnvanol

    Pnvanol

    Joined:
    Jan 11, 2016
    Posts:
    116
    Yes the gamepad is detected but for some reason the controls switch to keyboard and mouse, unless I move the stick a bit before spawning the virtual mouse
     
  9. Pnvanol

    Pnvanol

    Joined:
    Jan 11, 2016
    Posts:
    116
    Nevermind the problem was that I wasnt spawning the virtual cursor prefab all good now, thank you
     
    imaginationrabbit likes this.
  10. Tornar

    Tornar

    Joined:
    Dec 28, 2020
    Posts:
    87
    Two years have passed, and I've had to resort to this workaround since I encountered the same issue mentioned in the original post. When I attempt to activate the "Virtual Mouse" while using a gamepad and deactivate it when the player switches to a mouse, I still encounter the same error.

    Is there a more efficient or effective solution to make this work seamlessly?
     
  11. CleverAI

    CleverAI

    Joined:
    Jun 4, 2017
    Posts:
    38
    InputSystem is now version 1.7.0 and it still has this error.

    And using imaginationrabbit idea is not possible as the script will redo any changes immediately itself.

    You need to do this with the InputSystem package before:
    https://support.unity.com/hc/en-us/articles/9113460764052-How-can-I-modify-built-in-packages

    Then you can modify the script as imaginationrabbit said and the error is gone.

    But for me I found out that scaling the virtual mouse rect transform to 0 is better than to deactivate it. It fixed for me some other errors as well and still acts as if the virtual mouse is deactivated.
     
    Last edited: Feb 19, 2024
  12. Tymianek

    Tymianek

    Joined:
    May 16, 2015
    Posts:
    97
    any chances of this gettings fixed?

    edit: Instead of removing and adding the m_VirtualMouse you can DisableDevice and EnableDevice it instead and then it doesn't throw exceptions. :)
     
    Last edited: Mar 8, 2024
  13. Skyfall007r

    Skyfall007r

    Joined:
    May 19, 2017
    Posts:
    2

    Where do you call DisableDevice and EnableDevice?
     
  14. Tymianek

    Tymianek

    Joined:
    May 16, 2015
    Posts:
    97
    In a copy of a VirtualMouseInput.cs (renamed the class and file to VirtualMouseInput2) file that is inside my Assets folder instead of the Packages,

    Enable the device after the mouse is created or added in the OnEnable block

    Code (CSharp):
    1. // Add mouse device.
    2. if (m_VirtualMouse == null)
    3.     m_VirtualMouse = (Mouse)InputSystem.AddDevice("VirtualMouse");
    4. else if (!m_VirtualMouse.added)
    5.     InputSystem.AddDevice(m_VirtualMouse);
    6.  
    7. InputSystem.EnableDevice(m_VirtualMouse);
    upload_2024-3-21_12-39-57.png

    Modify the OnDisable, to use DisableDevice instead of RemoveDevice

    Code (CSharp):
    1. // Remove mouse device.
    2. if (m_VirtualMouse != null && m_VirtualMouse.added)
    3.     InputSystem.DisableDevice(m_VirtualMouse);
    upload_2024-3-21_12-44-48.png

    Remember to attach the copied VirtualMouseInput2 instead of the VirtualMouseInput. You can use the Debug mode of the inspector to replace the reference to the script file.
     
    Last edited: Mar 21, 2024