我收到错误 CS0236,我不知道该怎么办

问题描述 投票:0回答:1

嗨,我是 C# 的初学者,当我不断遇到这个问题时,我正在做一个学校项目。我试图将分数设置为玩家在该游戏中达到的最高分。我正在用 c# 编写代码,错误是字段初始值设定项无法引用非静态字段、方法或属性“scoreKeepScript.highestPoint”。

这是我的代码:

public class scoreKeepScript : MonoBehaviour
{
    
    public float highestPoint;
    public GameObject playerObject;
    public float currentPosition;
    public float maxLow = 50;
    public int highestPlayerPoint = Math.Round(highestPoint, 0);

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        currentPosition = playerObject.transform.position.y;

        if (currentPosition > highestPoint)
        {
            highestPoint = currentPosition;
        }
        else if (currentPosition < highestPoint - maxLow) 
        {
            Debug.Log("dede");
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }
}

我希望 HighestPlayerPoint 是最高点当前数字的四舍五入版本。希望大家有什么建议。

c# unity-game-engine rounding
1个回答
0
投票

您需要使用属性或方法:

public double HighestPlayerPoint
{
    get
    {
        return Math.Round(highestPoint, 0);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.