InputSystem throws InvalidOperationException
Hello guys I am struggling with the new input system.
I have set up inputActions + generated Script with the following action in actionmap "Player" (pseudocode):
MoveCamera (Action Type = Value, Control Type = Vector 2) {
WASD [Keyboard] (Composite Type = 2D Vector, Mode = Digital Normalized)
Left Stick [GamePad]
}
My script implements the created interface in the generated Input.cs.
My GameObject has the component PlayerInput, where I gave my empty method OnMoveCamera() as a reference to call.
I used other actions extensively already as type Button, Axis etc, all which work fine.
However, whenever I press [WASD] or use the left stick of my gamepad I get the following exception:
InvalidOperationException while executing 'performed' callbacks of 'Player/MoveCamera[/Keyboard/w,/Keyboard/s,/Keyboard/a,/Keyboard/d]'
UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)
InvalidOperationException: Cannot read value of type 'Single' from composite 'UnityEngine.InputSystem.Composites.Vector2Composite' bound to action 'Player/MoveCamera[/Keyboard/w,/Keyboard/s,/Keyboard/a,/Keyboard/d]' (composite is a 'Int32' with value type 'Vector2')
It seems to be an internal bug in the input system or I messed up my above input actions.
I had this issue as well, and it turned out that Visual Studio imported the "System.Numerics" namespace for Vector2 rather than "UnityEngine". Could that be your issue?
Answer by heatblayze · Jun 27, 2021 at 10:31 AM
Also had this issue while I was playing around with the SendMessages behaviour of PlayerInput. The cause of the issue for me was that I was trying to access the InputValue.isPressed property, which it seems is not applicable to Vector2 input bindings.
For Value types, the messages are sent every time a value is changed, so if you want to check for that type of behaviour with Vector2's you'll have to do some extra code yourself.
Alternatively you can use one of the other PlayerInput notification behaviours that instead gives you an InputAction.CallbackContext to check what the state is.