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

跟随Unity教程时遇3个C#代码错误,寻求解决方法

Unity输入控制器脚本错误修复

错误信息

  • Assets/Scripts/Controllers/PlayerController.cs(13,27): error CS0115: 'PlayerController.RetrieveMoveInput()': no suitable method found to override
  • Assets/Scripts/Controllers/PlayerController.cs(8,26): error CS0115: 'PlayerController.RetrieveJumpInput()': no suitable method found to override
  • Assets/Scripts/Controllers/PlayerController.cs(6,33): error CS0246: The type or namespace name 'InputController' could not be found (are you missing a using directive or an assembly reference?)

问题分析与修复

三个错误的核心原因是基类命名不匹配:

  1. 你定义的基类脚本类名为Input,但PlayerController试图继承InputController,导致系统找不到这个类(触发CS0246错误)。
  2. 由于继承的目标类不存在,override标记的方法找不到对应的抽象方法,进而触发CS0115错误。

另外,基类命名为Input会和Unity自带的UnityEngine.Input类冲突,这也是需要修正的潜在问题。

修改后的基类脚本(InputController.cs)

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

public abstract class InputController : ScriptableObject
{
    public abstract float RetrieveMoveInput();
    public abstract bool RetrieveJumpInput();
}

修改后的PlayerController脚本

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

[CreateAssetMenu(fileName = "PlayerController", menuName = "InputController/PlayerController")]
public class PlayerController : InputController
{
    public override bool RetrieveJumpInput()
    {
        // 加UnityEngine前缀避免命名冲突
        return UnityEngine.Input.GetButtonDown("Jump");
    }

    public override float RetrieveMoveInput()
    {
        return UnityEngine.Input.GetAxisRaw("Horizontal");
    }
}

完成以上修改后,所有错误应该会被解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 10:55:21