unity type_error ai_generated true

InvalidOperationException: Cannot read value of type 'float' from a control with value type 'Vector2'.

ID: unity/input-system-float-parameter-invalid

Also available as: JSON · Markdown · 中文
90%Fix Rate
85%Confidence
1Evidence
2023-08-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Unity 2022.3.12f1 active
Unity 2023.2.0b1 active
Input System 1.7.0 active

Root Cause

The Input System action is bound to a control that outputs a Vector2 (e.g., a joystick), but the code tries to read it as a single float, causing a type mismatch.

generic

中文

输入系统操作绑定到了输出 Vector2 的控件(例如摇杆),但代码试图将其作为单个 float 读取,导致类型不匹配。

Official Documentation

https://docs.unity3d.com/Packages/[email protected]/manual/ActionBindings.html

Workarounds

  1. 95% success Change the action's expected value type to match the control. For a Vector2 control, read the value as `Vector2` using `action.ReadValue<Vector2>()`. For example: `Vector2 move = playerInput.actions["Move"].ReadValue<Vector2>();`.
    Change the action's expected value type to match the control. For a Vector2 control, read the value as `Vector2` using `action.ReadValue<Vector2>()`. For example: `Vector2 move = playerInput.actions["Move"].ReadValue<Vector2>();`.
  2. 85% success If only one axis is needed, bind the action to a single-axis control (e.g., Keyboard key or button) instead of a Vector2 stick, and read as `float`.
    If only one axis is needed, bind the action to a single-axis control (e.g., Keyboard key or button) instead of a Vector2 stick, and read as `float`.
  3. 80% success Use `action.ReadValue<float>()` only for actions bound to controls that output a single float, such as an Axis control. Verify the binding in the Input Action Asset editor.
    Use `action.ReadValue<float>()` only for actions bound to controls that output a single float, such as an Axis control. Verify the binding in the Input Action Asset editor.

中文步骤

  1. 将操作的预期值类型更改为与控件匹配。对于 Vector2 控件,使用 `action.ReadValue<Vector2>()` 读取 Vector2 值。例如:`Vector2 move = playerInput.actions["Move"].ReadValue<Vector2>();`。
  2. 如果只需要一个轴,则将操作绑定到单轴控件(例如键盘按键或按钮),而不是 Vector2 摇杆,并作为 float 读取。
  3. 仅当操作绑定到输出单个 float 的控件(例如 Axis 控件)时,才使用 `action.ReadValue<float>()`。在输入操作资源编辑器中验证绑定。

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Changing the action type to 'Pass Through' without adjusting the control binding may still result in type mismatch if the control output type is incompatible.

  2. 90% fail

    Disabling and re-enabling the action map does not resolve the fundamental type mismatch between the expected value type and the control's output.