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

Unity3d Action Map脚本错误CS1061求助:找不到OnFoot定义

Unity InputSystem 脚本错误解决

动作映射
脚本

我遇到了以下错误,无法找到解决方法:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class InputManager : MonoBehaviour
{
    private PlayerInput playerInput;
    private Playerinput.OnFootActions onFoot;

    void Awake() 
    {
        playerInput = new PlayerInput();
        onFoot = playerInput.OnFoot;
    }
    void Update()
    {
        
    }
    private void OnEnable() 
    {
        onFoot.Enable();
    }
    private void OnDisable() 
    {
        onFoot.Disable();
    }
}

'PlayerInput' 不包含 'OnFoot' 的定义,也找不到可接受第一个参数类型为 'PlayerInput' 的可访问扩展方法 'OnFoot'(是否缺少 using 指令或程序集引用?)[Assembly-CSharp]


错误原因及解决步骤

  1. 大小写语法错误
    代码中Playerinput.OnFootActions的Playerinput是小写开头,C#是大小写敏感语言,需改为大写开头的PlayerInput(或你自定义的Input Action类名)。

  2. 核心问题:引用错误的类
    Unity内置的PlayerInput组件类不会自动生成OnFoot属性,OnFoot是你创建的Input Action Asset自动生成的类中的动作映射。解决方法:

    • 打开你的Input Action Asset(截图中的动作映射资源),在Inspector面板勾选Generate C# Class,点击Apply,Unity会生成对应类(类名默认和Asset文件名一致,比如PlayerControls)。
    • 修改代码中的类引用:
      // 替换成你自动生成的类名,比如PlayerControls
      private PlayerControls playerInput;
      private PlayerControls.OnFootActions onFoot;
      
      void Awake() 
      {
          // 实例化自动生成的Input Action类
          playerInput = new PlayerControls();
          onFoot = playerInput.OnFoot;
      }
      
    • 确保代码顶部引用了生成类的命名空间(若生成时指定了命名空间)。
  3. 额外注意
    若你想用Unity内置的PlayerInput组件,不要用new PlayerInput()实例化,应通过GetComponent<PlayerInput>()获取组件引用,再通过playerInput.actions["OnFoot"]访问动作映射。


内容的提问来源于stack exchange,提问作者Avishai Bagon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 09:05:24