【问题标题】:Method must have a return type (Unity 3D)方法必须有返回类型(Unity 3D)
【发布时间】:2019-04-02 06:07:44
【问题描述】:

我试图让我的播放器有一个恒定的速度前进,我在编译时遇到了这个错误。

public class Speed
{
    public static int movespeed = 1;
    public Vector3 userDirection = Vector3.right;
    public Start()
    {

    }

    public void Update()
    {
        transform.Translate(userDirection * movespeed * Time.deltaTime); 
    }
}

【问题讨论】:

  • 公共无效开始
  • 什么是Start() 方法?是否应该是构造函数,如果是,请将其更改为public Speed(),否则请为该方法提供返回类型。

标签: c# unity3d


【解决方案1】:

你忘记了 Start 方法的返回类型:

public void Start()

顺便说一句,您还忘记了对 MonoBehavior 的继承(如果您想将脚本附加到游戏对象);-)

公开课速度:MonoBehaviour

public class Speed : MonoBehaviour
{
    public static int movespeed = 1;
    public Vector3 userDirection = Vector3.right;

    public void Start()
    {
    }

    public void Update()
    {
        transform.Translate(userDirection * movespeed * Time.deltaTime); 
    }
}

【讨论】:

    【解决方案2】:

    您的 start 方法没有返回类型。改为分配一个。

    public void Start()
    {
    }
    

    顺便说一句,这是非常基本的,不应该属于这里。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-16
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 2015-12-15
      • 2020-02-16
      相关资源
      最近更新 更多