You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 09:15:42