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

Unity C#中“方法名预期”编译错误的修复求助

解决Unity C#编译错误CS0149:Method name expected

你的编译错误是语法错误导致的,问题出在第15行代码:

thePlayer.GetComponent<Animation>().Play()("Run");

这里错误地在Play()后多写了一对括号,正确的调用方式是把动画名称直接作为参数传入Play方法,就像你在else分支里调用Play("Idle")的写法一致。

修正后的完整代码如下:

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

public class CharControl : MonoBehaviour {
    
    public GameObject thePlayer;
    public bool isRunning;
    public float horizontalMove;
    public float verticalMove;
    
    void Update () {
        if (Input.GetButton("Horizontal") || Input.GetButton("Vertical")) 
        {
            thePlayer.GetComponent<Animation>().Play("Run");
            horizontalMove = Input.GetAxis("Horizontal") * Time.deltaTime * 150;
            verticalMove = Input.GetAxis("Vertical") * Time.deltaTime * 8;
            isRunning = true;
            transform.Rotate(0, horizontalMove, 0);
            transform.Translate(0, 0, verticalMove);
        }
        else
        {
            thePlayer.GetComponent<Animation>().Play("Idle");
            isRunning = false;
        }
    }
}

修改后编译器能正确识别方法调用,CS0149错误会消失,你可以正常进入Unity运行模式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 21:50:20