Score

I try to learn C# and Unity, I am beginner
I`m working to a little game for me and i want to do a high score but i really don t know how
I did the score and it work, not how i want, but it work

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
  public Transform player;
  public Text scoreText, highScoreText;
  public int score, highScore;
 
 

    // Update is called once per frame
    void Update()
    {
        scoreText.text = player.position.z.ToString("0");
     
     
    }
    public void UpdateHighScore(){
        if(score > highScore){
            highScore = score;

            highScoreText.text = highScore.ToString();

            PlayerPrefs.SetInt("HighScore", highScore);
        }
    }
 
}

that is my script.
and the other problem i have is…
for example when i finish the first level i have 500score, and when i continue to level 2 my score is 0
what i have to do to continue in level 2 with 500score for example? i tryed to do it by myself but is not working, how i said im a really beginner Is a game like Brackeys tutorials(i guess many of you know that videos). But i try to put in this game a lot of things, to see how it works everything, score, menu,respawn, ads etc...isnt a real game, is just a experiment

Thank you for help

try not to blame a script that does nothing wrong, but look out for which script is the real imposter:roll_eyes:

because to me who ever do Level switching task is petty sus:eyes:

and who ever use UpdateHighScore() also sus too:eyes:

You can make your variable static:
This will make it accessible across different scripts. I used it here to keep count of enemies so I knew when to load the next scene. Hope this helps!
-Z

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class destroy : MonoBehaviour
{
public static int opCount = 3; //to access from another script, do FILENAME.Varname
// so for me it was destory.opCount

private void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
opCount–;
Debug.Log(opCount);
if (opCount <= 0)
{
SceneManager.LoadScene(3);
}
}
}

Tracking simple high score / low time single-entry leaderboard:

For the highest score: Unity High score tracking - Pastebin.com

Usage:

TheBest.RecordScoreIfHigher( lastGamePlayScore);

For the lowest time: Unity best time tracking - Pastebin.com

Usage:

TheBest.RecordTimeIfLower( lastGamePlayTime);

To retrieve the best score or time, use one of these:

int bestScore = TheBest.BestScore;
float bestTime = TheBest.BestTime;

Full usage example: