Can someone help me with my highscore script?

I created a highscore system but for some reason it is not working. The highscore scripts pulls data from another script using playerprefs but it isnt showing the highscore. If someone can help me out that would be great. (I am not the best programmer)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HighScore : MonoBehaviour
{
public Text highScoreText;
public float highScore;
public float currentScore;

public void Start()
{
    PlayerPrefs.GetFloat("highScore");

    
}

public void Update()
{
    if (currentScore > highScore)
    {

        currentScore = highScore;

        highScoreText = GetComponent<Text>();

        highScoreText.text = highScore.ToString();
        
        PlayerPrefs.SetFloat("highScore", highScore);
    }

}

public void FixedUpdate()
{
    currentScore = PlayerPrefs.GetFloat("currentTime");

    
    highScore = PlayerPrefs.GetFloat("highScore");

}

}

this is the script with the timer where I pull data from for the highscore

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreCounter : MonoBehaviour
{

public float timeStart = 0;
public Text textBox;

public float currentTime;


void Start()
{
	textBox.text = timeStart.ToString();
}


void Update()
{
	timeStart += Time.deltaTime;
	textBox.text = Mathf.Round(timeStart).ToString();

	currentTime = Mathf.Round(timeStart);

	PlayerPrefs.SetFloat("currentTime", currentTime);

	
}

}

There are few problems with those scripts.

Problems in the HighScore script

  • At the Start Event Function you’re using GetFloat to get the highscore but you never assign it?
  • You shouldn’t get the “highScoreText” Component each time you set new high score, just initialize it in using the Awake Event Function.
  • Why do you reassign, the currentScore and the highScore in the FixedUpdate Event Function? you already assigned the value on update, even though, you should only assign it once at awake/start.

Problems in the ScoreCounter script

  • You’re rounding the same value twice “Mathf.Round(timeStart)” just round it once and save it in a variable to use it again.
  • You shouldn’t communicate between 2 scripts using the PlayerPrefs, just get a reference of the script.

This is a fix of the code:

public class HighScore : MonoBehaviour
{
    public ScoreCounter ScoreCounter;

    public Text highScoreText;
    public float highScore;

    string highScoreKey = "highScore";

    public void Awake()
    {
        highScoreText = GetComponent<Text>();
        highScore = PlayerPrefs.GetFloat(highScoreKey);
    }

    public void Update()
    {
        if (ScoreCounter.currentTime > highScore)
        {
            highScore = ScoreCounter.currentTime;
            highScoreText.text = highScore.ToString();
            PlayerPrefs.SetFloat(highScoreKey, highScore);
        }
    }
}

public class ScoreCounter : MonoBehaviour
{
    public float timeStart = 0;
    public Text textBox;
    public float currentTime;

    void Start()
    {
        textBox.text = timeStart.ToString();
    }

    void Update()
    {
        timeStart += Time.deltaTime;
        float roundedTime = Mathf.Round(timeStart);
        currentTime = roundedTime;
        textBox.text = roundedTime.ToString();
    }
}

In the class HighScore in the update function you use PlayerPrefs.SetFloat(“highScore”, highScore)
Shouldn’t this be PlayerPrefs.SetFloat("highScore, currentScore)? Because it looks like you’re now updating the highscore to what it already was