Overwritting score with GUI

I have the following code with which I am showing the current score.

void OnGUI()
	{   
		GUI.Label (new Rect (10, 40, 200, 100), "Level: " + ((int) Application.loadedLevel - 1 )+ '

');
if (Application.loadedLevel == 2)
GUI.Label (new Rect (10, 60, 200, 100), "Score: " + coins + “/ 18”);
if (Application.loadedLevel == 3)
GUI.Label (new Rect (10, 60, 200, 100), "Score: " + coins + “/ 25”);
if (Application.loadedLevel == 4)
GUI.Label (new Rect (10, 60, 200, 100), "Score: " + coins + “/ 39”);

	}

(one coin, one point)
The problem is that, when a coin is collected the points are overwritting.

How can I fix this?

Just as RedDevil said, in your script you’re using the same variable for every level, so let’s say the player finished the first level, once he starts the second the value of the coins will begin with that of the first level instead of 0. You can fix this in two ways:

  1. For each level have a coin variable ex: coinLVL1, coinLVL2…

  2. Once the next level starts return the coin variable to 0. You can then save the score of each in an array of ints or floats in order to display them.
    For example
    public int coins;
    public int nb = 0;
    public int coinScore[nb];
    public bool isFinished = false;

    voidUpdate{
    if(isFinished)//add your conditions
    nb++;
    coinScore[nb] = coins;
    coins = 0;
    isFinished = false;
    }
    void OnGUI{
    GUI.Label(new Rect (10, 60, 200, 100), "Score: " + coinScore[1] + “/ 18”;
    }

Hope this helps