Search code examples
c#unity-game-enginegetter-setter

Unity Debug Error: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement


I am trying to create a 2D hit ball game. I want to access an int variable (public int ScoreTask = 10;) which locates in another script. However, when I debug the code, I get these errors:

error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

error CS0103: The name `ScoreTask' does not exist in the current context

Here is my script:

public class Countdown : MonoBehaviour 
{   
    private Ball scoreScript;
    public GameObject ScoreBoard;

    void Start () 
    {
        scoreScript = ScoreBoard.GetComponent<Ball> ();
    }
    void Update () 
    {
        GetScore ();
    }
    void GetScore() 
    {
        scoreScript.ScoreTask;

        if (timeLeft == 0 && ScoreTask != 0)
            Application.LoadLevel("GameOver");
    }
}

Solution

  • Simply do:

    void GetScore() 
    {
        if (timeLeft == 0 && scoreScript.ScoreTask != 0){
            Application.LoadLevel("GameOver");
        }
    }
    

    The issue is that you were asking what ScoreTask was in your if condition, but it doesn't know what ScoreTask is without the context of where it comes from, i.e. scoreScript.ScoreTask