- Home /
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?
I see you are using PlayerPrefs to save the score. If you want to use PlayerPrefs.GetFloat in the new scene to keep adding up, you have to change how you are saving it. You are currently saving only your current zPos. You'll need an extra variable like totalScore and keep adding to that. Then set and get that between scenes.
But how can you calculate them as : "previous Position " and "Current Position" at the same time? This is my noobish attempt
public Transform player;
public Text scoreText ;
float zPos;
static float totalScore;
public int sceneCounter;
void Update()
{
sceneCounter = Scene$$anonymous$$anager.GetActiveScene().buildIndex;
zPos = player.position.z;
scoreText.text = totalScore.ToString("0");
PlayerPrefs.SetFloat("theScore", zPos);
totalScore = zPos;
if (sceneCounter == sceneCounter + 1)
{
totalScore = totalScore + PlayerPrefs.GetFloat("theScore");
}
Note: The answer might be simple and clear but i am new to c# with unity
You can use Start() to get what the previous score was when starting a new scene. float previousScore
void Start()
{
previousScore = PlayerPrefs.GetFloat("theScore");
}
Then you can get the totalScore by adding previousScore and zPos.
Answer by Sinister-Design · Feb 22, 2019 at 09:48 PM
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.)