How to keep position based score to next scene

The score of my game is based on the position of the player in the z axes, but i can’t figure out how to keep that score to next scene and start adding up.

Score Script

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;


public class Score : MonoBehaviour
{

    public static Transform player;
    public Text scoreText ;
    static float zPos;


    void Update()
    {

        zPos = 1 + player.position.z / 2;
        scoreText.text = zPos.ToString("0");
        PlayerPrefs.SetFloat("theScore", zPos);

        if (FindObjectOfType<GameManager>().GameEnds == true)
        {
            Destroy(scoreText);
        }
     
    }
}

GameManager Script

using UnityEngine;
using UnityEngine.SceneManagement;


public class GameManager : MonoBehaviour
{
    public bool GameEnds = false;

    public GameObject levelComplete;

    public GameObject GameOverAn;

    public GameObject pauseMenuUI;

    public static bool pauseMenu;

    public void GameOver()
    {


        if (GameEnds == false)
        {
            GameEnds = true;
            GameOverAn.SetActive(true);
            Destroy(levelComplete);

        }
    }

    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (pauseMenu)
            {
                Resume();
            }
            else
            {
                Pause(); ;
            }

        }

    }

    public void Resume()
    {
        pauseMenuUI.SetActive(false);
        Time.timeScale = 1f;
        pauseMenu = false;
    }

    void Pause()
    {
        pauseMenuUI.SetActive(true);
        Time.timeScale = 0f;
        pauseMenu = true;
    }

    void RestartGame()
    {

        SceneManager.LoadScene(SceneManager.GetActiveScene().name);

    }

    public void NextLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    public void LevelComplete()
    {

        levelComplete.SetActive(true);
        Destroy(GameOverAn);

    }
}

I have tried lots of things like DontDestroyOnLoad() or making the float static, and other… Does the fact that my score is based on position means that i can’t keep it in the next scene?

What you want is a static class that you can use to store (and access) data globally, regardless of scene. Something like:

public static class GlobalData {

    public static float score = 0;

    public static void incrementScore ( float amount ) {
        score += amount;
    }

    public static float getScore () {
        return score;
    }
}

Once you’ve created this class in a script file, you can easily update the score by calling GlobalData.incrementScore(), and retrieve it by calling GlobalData.getScore() in the next scene.

(To be clear: your existing Score class does not serve this function because it is not static, and extends MonoBehaviour. To use a class like this with your existing scripts, you’d probably want to make the NextLevel() method call GlobalData.incrementScore( Score.zPos ); right before it calls the SceneManager.)