如何修复unity c#上预期的方法名称和编译器错误?



这是我关于堆栈溢出的第一个问题。

这是我的代码:

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;
}
}
}

我得到了一些错误信息,如

AssetsScriptsCharactersCharControl.cs(15,13):错误CS0149: Method name expected

所有编译器错误必须在你进入游戏模式之前修复!UnityEditor。SceneView: ShowCompileErrorNotification ()

我不知道如何解决这个问题,所以我真的很感激一些帮助。提前谢谢。

我刚刚开始制作《侠盗猎车手》之类的unity系列。我正在跟随吉米·维加斯的教程。到目前为止我还没有遇到任何问题。我在编码(c#)部分有一些错误。

你的代码有语法错误。

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

你试图在thePlayerGameObject的Animation组件上调用Play()方法,但是你没有为该方法提供参数,你只是为Play()方法提供了空括号,并在单独的花括号内传递字符串。

试试这个:

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

请注意,Animation组件在Unity中已被弃用,您应该考虑使用Animator组件。

相关内容

最新更新