InvalidOperationException: 无法读取动作'Jump'的值,因为动作映射'Gameplay'未启用
InvalidOperationException: Cannot read value for action 'Jump' because the action map 'Gameplay' is not enabled
ID: unity/input-system-action-map-not-enabled
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Unity 2022.3 | active | — | — | — |
| Unity 2023.2 | active | — | — | — |
| Input System 1.5.0 | active | — | — | — |
| Input System 1.6.0 | active | — | — | — |
根因分析
包含该动作的输入动作映射在尝试读取其值之前未启用,通常是因为未对映射或InputActionAsset调用Enable()。
English
The Input Action Map containing the action is not enabled before trying to read its value, typically because Enable() was not called on the map or the InputActionAsset.
官方文档
https://docs.unity3d.com/Packages/[email protected]/manual/ActionMaps.html解决方案
-
在Awake()或Start()中启用动作映射,然后再读取任何输入:inputActionAsset.FindActionMap("Gameplay").Enable(); 或使用inputActionAsset.Enable()启用所有映射。 -
如果使用PlayerInput组件,在检查器中设置默认映射为所需映射,并确保组件在游戏对象上已启用。
-
在每个动作上设置回调后使用InputAction.Enable(),但确保父映射也已启用以避免冲突。
无效尝试
常见但无效的做法:
-
90% 失败
Overrides bypass the normal input flow and can cause stale values or conflicts with actual input devices. The action map still remains disabled.
-
85% 失败
This defeats the purpose of using action maps for contextual input (e.g., UI vs Gameplay) and may break input switching logic.
-
80% 失败
Enabling individual actions without the parent map can lead to inconsistent state and may not register input correctly if the map is disabled.