错误非静态字段,方法或属性'gameManager.CompleteLevel()'需要对象引用

时间:2019-05-05 14:45:37

标签: visual-studio unity3d

我正在学习Brackeys的Unity游戏教程,由于某种原因,我的代码返回了此错误-非静态字段,方法或属性'gameManager.CompleteLevel()'需要对象引用< / p>

这是一些代码/上下文

public class gameManager : MonoBehaviour
{
    bool gameHasEnded = false;
    public float restartdelay = 11f;

    public void CompleteLevel()//here is the Error
    {
        Debug.Log("LEVEL COMPLETED");//printing this to make sure it works
    }

    public void gameEnd()
    {
        if (gameHasEnded == false)
        {
            gameHasEnded = true;
            Debug.Log("GAME OVER");
            Invoke("Restart", 2f);//here making a delay
        }
    }
}

1 个答案:

答案 0 :(得分:0)

Error- An object reference is required for the non-static field, method, or property 'gameManager.CompleteLevel()'的发生是因为您试图直接访问某些内容,而不使其成为静态或不使用引用。您需要确定是否要将此方法或类设为static或为其创建引用。

您只需要将其设置为静态

public static void CompleteLevel()//here is the Error
{
    Debug.Log("LEVEL COMPLETED");//printing this to make sure it works
}