i get the error CS0236 and i dont know what to do

42 Views Asked by At

Hi i'm a beginner in c# and I was working on a school project when i kept running into this issue. i was trying to set the score to the highest point the player reached that game. I'm writing the code in c# and the error is A field initializer cannot reference the non-static field, method, or property 'scoreKeepScript.highestPoint'.

This is my code:

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

I would like the HighestPlayerPoint to be a rounded version of the number that highestPoint is currently. i hope you guys have any suggestions.

1

There are 1 best solutions below

0
vhr On

You need to use a property or a method:

public double HighestPlayerPoint
{
    get
    {
        return Math.Round(highestPoint, 0);
    }
}

or

public double HighestPlayerPoint => Math.Round(highestPoint, 0);