Coin value added to preference.

I am making a game where you get kills, and after, lets say 1 kill, you get a coin etc and it will save to the preference file.

Here is my problem, I get a couple kills, I earn, lets say 30 coins. When I restart the round and get a kill, it goes back down to 10 coins when I get my first kill, I want the value to ADD upon the existing value.

using UnityEngine;
using System.Collections;

public class Coins : MonoBehaviour {
		// Use this for initialization
	static int coins01;
	void Start() {
		int coins01 = PlayerPrefs.GetInt ("Coins");
		if (!PlayerPrefs.HasKey ("Coins")) {
			PlayerPrefs.SetInt ("Coins", 0);
			PlayerPrefs.Save ();
			
		}
	}


	public static void Kills() {
		
		//10
		if (Health.kills == 1) {
			coins01 = coins01 + 10;
			PlayerPrefs.SetInt ("Coins",coins01 );
			PlayerPrefs.Save ();

		}			
		//20
		if (Health.kills == 2) {
			coins01 = coins01 + 20;
			PlayerPrefs.SetInt ("Coins",coins01);
			PlayerPrefs.Save ();

		}//50
		if (Health.kills == 3) {
			coins01 = coins01 + 25;
			PlayerPrefs.SetInt ("Coins", coins01);
			PlayerPrefs.Save ();

		}//75
		if (Health.kills == 75) {
			coins01 = coins01 + 35;
			PlayerPrefs.SetInt ("Coins", coins01);
			PlayerPrefs.Save ();

		}//110
		if (Health.kills == 110) {
			coins01 = coins01 + 45;
			PlayerPrefs.SetInt ("Coins", coins01);
			PlayerPrefs.Save ();

		}//155
		if (Health.kills == 155) {
			coins01 = coins01 + 65;
			PlayerPrefs.SetInt ("Coins", coins01);
			PlayerPrefs.Save ();

		}
	}
}

Hi again, I assume here that you end the scene at the end of a round, then load a new scene for the next round. If you are not doing this and everything is all in one scene then you should not be using player prefs for this.

  1. In your Start() function write :

    void Start() {

     if (PlayerPrefs.HasKey("Coins")) {
         coins01 = PlayerPrefs.GetInt("Coins");     
     } else {
         coins01 = 0;
     }
    

    }

  2. Then when the round ends(before you load the next scene) call this :

    void Save_Coins() {

     //Set the Player Pref Coins.
     PlayerPrefs.SetInt("Coins", coins01);
     
     //Save.
     PlayerPrefs.Save();
    

    }

When the current game is over you must delete the “Coins” player pref, otherwise it will pick this up and carry on where it left off at the start of the next game.