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
相关产品推荐
相关产品推荐

