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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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.
     
  2. LeandroExHuMeD

    LeandroExHuMeD

    Joined:
    Apr 6, 2016
    Posts:
    5
    I'm having this same problem, did you solve it? :/
     
  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
    Tornar likes this.
  6. Pnvanol

    Pnvanol

    Joined:
    Jan 11, 2016
    Posts:
    110

    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:
    110
    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:
    110
    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:
    86
    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?