errer only assignment call increment decrement and new object expressions can be used as a statemen

Hey I have the following Script. Almost Everything is working fine but in Line 87 there is the error-code: “errer CS0201: only assignment call increment decrement and new object expressions can be used as a statement”.
I really don´t know what the problem is. Please help and say what I should make instead. Thx for every answer.

Here the script:

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour
{
    public bool isCounting = true;
    public float timer = 0f;
    public int score = 0;
    public int upgradePoints;
    public int hiScore;
    private GUIStyle guiStyle = new GUIStyle();
    public GameObject DethScreen;
    public bool showScoreInTheMiddle = false;
    public bool takeScore;
    public bool scoreIsTakable;
    public int requiredPoints;
    public bool upgradable;
    public bool notEnoughPointsGUI;
    public float notEnoughPointsGUIShowCounter;
    public bool showGUI;
    public bool showHealthOnScreen;
    public int healthInThisScript;

    void Start()
    {
        hiScore = PlayerPrefs.GetInt("HighScore");
        requiredPoints = PlayerPrefs.GetInt("requiredPoints");
        upgradePoints = PlayerPrefs.GetInt("upgradePoints");
        scoreIsTakable = true;
        takeScore = false;

        GameObject kugel = GameObject.Find("Kugel");
        KugelCollider kugelCollider = kugel.GetComponent<KugelCollider>();
    }



    void Update()
    {
        if (isCounting)
        {
            timer += Time.deltaTime;
            if (timer > 1f)
            {
                score += 1;
                timer -= 1f;
            }
        }
        if (DethScreen.activeInHierarchy)
        {
            isCounting = false;
            takeScore = true;
            showHealthOnScreen = true;
        }else
        {
            showHealthOnScreen = false;
        }

        if (takeScore == true)
        {
            if (scoreIsTakable == true)
            {
                upgradePoints = upgradePoints + score;
                scoreIsTakable = false;
            }
        }
        //Upgrade Funktion beginnt hier
        playerScript.health = healthInThisScript;

        if (upgradePoints >= requiredPoints)
        {
            upgradable = true;
        }else
        {
            upgradable = false;
        }

        PlayerPrefs.SetInt("requiredPoints", requiredPoints);
        PlayerPrefs.SetInt("upgradePoints", upgradePoints);
    }

    public void upgradeHealthButton()
    {
        if (upgradable == true)
        {
            requiredPoints = requiredPoints * 2;
            playerScript.healthInThisScript + 10; //HERE IS THE PROBLEME!!!
            upgradePoints = upgradePoints - requiredPoints;
        }else
        {
            notEnoughPointsGUI = true;
        }
    }


    void FixedUpdate()
    {
        if (score > hiScore)
        {
            hiScore = score;
            PlayerPrefs.SetInt("HighScore", hiScore);
        }


        if (DethScreen.activeInHierarchy)
        {
            showScoreInTheMiddle = true;
        }

        //Upgrade Funktion beginnt hier
        if (notEnoughPointsGUI == true)
        {
            notEnoughPointsGUIShowCounter += 1f;
            showGUI = true;
            notEnoughPointsGUI = false;
        }else
        {
            notEnoughPointsGUIShowCounter = 0;
        }

        if (notEnoughPointsGUIShowCounter >= 3)
        {
            showGUI = false;
        }
    }



    void OnGUI()
    {

        guiStyle.fontSize = 60;
        GUI.Label(new Rect(105, 22, 100, 100), score.ToString(), guiStyle);
        GUI.Label(new Rect(205, 22, 100, 100), hiScore.ToString(), guiStyle);

        if (showScoreInTheMiddle == true)
        {
            GUI.Label(new Rect(515, 290, 100, 100), score.ToString(), guiStyle);
        }

        if (showHealthOnScreen == true)
        {
            GUI.Label(new Rect(105, 22, 100, 100), playerScript.health.ToString(), guiStyle);
            GUI.Label(new Rect(105, 22, 100, 100), upgradePoints.ToString(), guiStyle);
        }
    }
}

you are not assigning the value to your variable …there is no “=” assignment operator

Just to add to this.
Use ‘+=’ which adds the value and assigns it back to the variable.

Thx for the answer :slight_smile: when I make the “+=”, there are 4 errors :confused: i can write the errors later

Well thanks but then it says in the same line: playerScript does not exist in teh current context

To help you understand the original problem, your line (statement) doesn’t do anything. It performs an operation and doesn’t do anything with the result, so C# doesn’t recognise it as valid code. This makes sense, as doing things that don’t affect anything at all seems like it would be a waste of both your time and the computer’s. Look here Statements - C# | Microsoft Learn to find what C# considers to be valid statements.

https://msdn.microsoft.com/en-us/library/t8zbaa6f.aspx

this is common compiler errors which you should study by google-ing the error code or text , msdn is big help here when you are learning , i reccomend anyone to learn WHAT an error means when you get one , it will help you in the future .

So on this line you get “playerScript does not exist in the current context”

After reading through I decided to tidy it up manually just looking for things that may of be issue later.

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour
{
    //ORGANISED VARIABLES BY TYPE
    // BOOLS ARE FALSE BY DEFAULT
    public bool isCounting = true;
    public bool showScoreInTheMiddle;
    public bool takeScore;
    public bool scoreIsTakable;
    public bool upgradable;
    public bool notEnoughPointsGUI;
    public bool showGUI;
    public bool showHealthOnScreen;

    // FLOATS AND INTS ARE 0 BY DEFAULT
    // YOU HAD SOME SAYING '= 0' AND SOME SAY NOTHING.
    // GOT RID OF THE ODD '= 0' FOR CONSISTENCY.
    public float notEnoughPointsGUIShowCounter;
    public float timer;
    public int requiredPoints;
    public int healthInThisScript;
    public int score;
    public int upgradePoints;
    public int hiScore;
    private GUIStyle guiStyle = new GUIStyle();
    public GameObject DethScreen;
    // UNLESS THIS SCRIPT IS STATIC, YOU SHOULD HAVE REFERENCE TO AN INSTANCE OF IT!
    // ASSIGN IT IN THE INSPECTOR!
    public playerScript pScript;

    void Start()
    {
        RetrievePlayerPrefs();
        scoreIsTakable = true;
        takeScore = false;
        GameObject kugel = GameObject.Find("Kugel");
        // KugelCollider?
        // IS THAT A SCRIPT OR AN ACTUAL COLLIDER?
        // IF ITS A BUILT-IN COLLIDER SHOULDN'T IT BE IN ENGLISH?
        KugelCollider kugelCollider = kugel.GetComponent<KugelCollider>();
    }
    void Update()
    {
        if (isCounting) {
            Counting();
        }
        if (DethScreen.activeInHierarchy) {
            isCounting = false;
            takeScore = true;
            showHealthOnScreen = true;
        }
        else {
            showHealthOnScreen = false;
        }

        if (takeScore == true) {
            if (scoreIsTakable == true) {
                upgradePoints = upgradePoints + score;
                scoreIsTakable = false;
            }
        }
        //Assign health to the specific instance of the script
        pScript.health = healthInThisScript;
        if (upgradePoints >= requiredPoints) {
            upgradable = true;
        }
        else {
            upgradable = false;
        }
  
        UpdatePlayerPrefsPoints();
    }
    // TIDIED THE CODE INTO SEPERATE METHODS BELOW.
    void Counting()
    {
        timer += Time.deltaTime;
            if (timer > 1f) {
                score += 1;
                timer -= 1f;
            }
    }
    void RetrievePlayerPrefs()
    {
        hiScore = PlayerPrefs.GetInt("HighScore");
        requiredPoints = PlayerPrefs.GetInt("requiredPoints");
        upgradePoints = PlayerPrefs.GetInt("upgradePoints");
    }
    // WHY ARE YOU UPDATING PLAYERPREFS EVERY FRAME?
    // ONLY UPDATE WHEN THEY CHANGE.
    void UpdatePlayerPrefsPoints()
    {
        PlayerPrefs.SetInt("requiredPoints", requiredPoints);
        PlayerPrefs.SetInt("upgradePoints", upgradePoints);
    }
    void UpdatePlayerPrefsHighScore()
    {
        hiScore = score;
        PlayerPrefs.SetInt("HighScore", hiScore);
    }
    // METHOD NAMES USE PascalCase not camelCase.
    // https://msdn.microsoft.com/en-gb/library/x2dbyw72(v=vs.71).aspx
    public void upgradeHealthButton()
    {
        if (upgradable == true) {
            requiredPoints = requiredPoints * 2;
            pScript.healthInThisScript + 10; //HERE IS THE PROBLEME!!!
            upgradePoints = upgradePoints - requiredPoints;
        }
        else {
            notEnoughPointsGUI = true;
        }
    }
    // FIXEDUPDATE?
    // USUALLY USED FOR PHYSCIS!
    // SUGGEST CHANGE TO UPDATE UNLESS I'M MISTAKEN.
    void FixedUpdate()
    {
        if (score > hiScore) {
            UpdatePlayerPrefsHighScore();
        }

        if (DethScreen.activeInHierarchy)
        {
            showScoreInTheMiddle = true;
        }

        if (notEnoughPointsGUI == true)
        {
            notEnoughPointsGUIShowCounter += 1f;
            showGUI = true;
            notEnoughPointsGUI = false;
        }
        else
        {
            notEnoughPointsGUIShowCounter = 0;
        }

        if (notEnoughPointsGUIShowCounter >= 3)
        {
            showGUI = false;
        }
    }
    // OLD OnGUI SYSTEM!
    // CONSIDER CHANGING TO NEW UI SYSTEM.
    void OnGUI()
    {
        guiStyle.fontSize = 60;
        GUI.Label(new Rect(105, 22, 100, 100), score.ToString(), guiStyle);
        GUI.Label(new Rect(205, 22, 100, 100), hiScore.ToString(), guiStyle);
        if (showScoreInTheMiddle == true) {
            GUI.Label(new Rect(515, 290, 100, 100), score.ToString(), guiStyle);
        }

        if (showHealthOnScreen == true) {
            GUI.Label(new Rect(105, 22, 100, 100), pScript.health.ToString(), guiStyle);
            GUI.Label(new Rect(105, 22, 100, 100), upgradePoints.ToString(), guiStyle);
        }
    }
}

Of note is Line 35.
Make sure you assign it your playerScript component within the inspector.
I dont know whether this will solve your issue though.
If issues still remain then copy and paste the whole error message here and I will check over it again.

EDIT:
Just noticed that for some reason the unity forum compacted the code a little resulting it in being slightly different from what I pasted in lol.
It got rid of a couple of empty lines I used to split thing up a little better.

EDIT #2:
Updated to input my empty lines again!
f*** you unity forum formatting.

1 Like

Thank you very much to your awesome work! It is so great that you did all the stuff!

1 Like