Unity新输入系统报错MissingMethodException:找不到OnMove函数如何解决?
解决Unity新输入系统MissingMethodException: PlayerMovement.OnMove错误
这个错误的核心原因是Player Input组件无法访问到OnMove方法,以下是具体的修复步骤:
1. 修改方法访问权限
当前代码里的OnMove是private权限,Player Input组件无法调用私有方法,需要改成public:
// 把private改为public public void OnMove(InputValue value) { moveInput = value.Get<Vector2>(); }
2. 检查Player Input组件设置
- 确保组件的Behavior选项设置为
Invoke Unity Events或Send Messages:- 选
Invoke Unity Events时,在对应Move动作的事件列表里,直接绑定PlayerMovement脚本的OnMove方法,注意参数要匹配InputValue类型; - 选
Send Messages时,要保证方法名称完全匹配(包括大小写)。
- 选
- 确认Input Action Asset中的Move动作类型是
Value -> Vector2,和方法接收的参数类型一致。
3. 基础排查
- 确保PlayerMovement脚本正确挂载到Player对象上,且Player Input组件的
Player Input引用的是该GameObject; - 清理Unity缓存:菜单栏
Edit->Clear All PlayerPrefs,重启Unity; - 重新生成输入系统代码:菜单栏
Edit->Project Settings->Input System Package->Regenerate Input System Code。
修改后的完整代码
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class PlayerMovement : MonoBehaviour { [SerializeField] float runSpeed = 10f; Vector2 moveInput; Rigidbody2D myRigidbody; void Start() { myRigidbody = GetComponent<Rigidbody2D>(); } void Update() { Run(); } public void OnMove(InputValue value) { moveInput = value.Get<Vector2>(); } void Run() { Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidbody.velocity.y); myRigidbody.velocity = playerVelocity; } }
内容的提问来源于stack exchange,提问作者Ardic
相关产品推荐
相关产品推荐

