Problems implementing Highscore Saves

Hello peeps :slight_smile:
I am currently developing a game for mobile devices which is heavily score based. We have a scoring system on the level itself but we are trying to add a simple system where the game will save your highest score onto the main menu however we seem to be having a problem with this.
We understand this question has been asked a lot and we have tried using a couple of peoples solutions but we have had no success as of yet.

We understand we need to use PlayerPrefs and have tried to implement it into our code, but the problem is that it saves the score, even if it is less than the old high score.

using UnityEngine;
using System.Collections;

public class EnemyMove : MonoBehaviour {

 float speed = 3.0f; // move speed
 public static int score = 0;
 public GameObject Explosion;
 int oldHighscore;
 void Update(){
 	if (GameObject.FindGameObjectsWithTag ("Player") != null && PointAndFire.Hit >= 1) {
 	 GameObject target = GameObject.FindGameObjectWithTag ("Player");
 	 transform.position = Vector3.MoveTowards (transform.position, target.transform.position, speed * Time.deltaTime);

 	 } else if (GameObject.FindGameObjectsWithTag ("Player") == null) {

 	 }

 	if (score > oldHighscore) {
 	 PlayerPrefs.SetInt ("highscore", score);
 	 oldHighscore = score;
 	 }
 	 }

 void OnCollisionEnter (Collision col)
 {
  if(col.gameObject.name == "bullet 2(Clone)")
 	{
 	 Destroy(col.gameObject);
 	 score += 1;
 	 Debug.Log(score);
 	 Instantiate (Explosion,transform.position, transform.rotation);
 	}

}

 void OnCollisionPlayer (Collision col)
 {
 	if(col.gameObject.tag == "Player")
 	{
 	 Destroy(gameObject);
  
 	  
 	}
 }

}

Any help would be greatly appreciated, Thanks!

Your oldHighScore was 0 when you start the game, so whatever you have in your PlayerPrefs is gone (so your high score from the last game you played a while back is gone for good).

void Update(){
   if (...) {
      ...
   }
 
   //In this if-statement, at your first playthrough, whatever
   //score you get, it will always be saved as the new high
   //score because you did not compare it to the high score
   //stored in the PlayerPrefs
   if (score > oldHighscore) {
      PlayerPrefs.SetInt ("highscore", score);
      oldHighscore = score;
   }
}

Try grabbing the score from playerprefs like so (this should fix it):

void Start() {
    //Get the high score in the playerprefs
    oldHighscore = PlayerPrefs.GetInt("highscore", 0); 
}