- Home /
How to keep track of score between Scenes?
This problem is really bugging me, any help would be appreciated!
Script #1
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Score : MonoBehaviour
{
public 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);
}
}
}
Script #2
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);
}
}
Note: I am a beginner casually looking for answers from similar questions, but nothing that i found was helpful (since i am not very familiar with complicated code).
Answer by jstopyraIGG · Feb 21, 2019 at 08:06 PM
You can tell Unity not to delete certain objects when scenes are loaded and unloaded
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
//this game object will not get destroyed between scene loading
void Awake()
{
DontDestroyOnLoad(gameObject);
}
You have to be careful with using this. Because if an object is not destroyed, you might get duplicates of it, because a new one will get spawned when a scene loads. You might want to use Singletons.
https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial/writing-game-manager
Answer by poupakos1 · Feb 21, 2019 at 08:40 PM
But the score is based on the position of z axes , so either way it'll go back to 0, because my player always starts at position 0 of the z axes.
I see, All you have to do is load the score
Vector3 playerPos = player.position;
playerPos .z = PlayerPrefs.GetFloat("theScore", zPos);
player.position = playerPos ;
Answer by telecaster · Feb 04 at 08:30 PM
I know I might get alot of hate for this, but could a singleton be the answer? https://gamedevbeginner.com/singletons-in-unity-the-right-way/
Your answer
Follow this Question
Related Questions
How to increase score by one per second when you are holding an object 2 Answers
Displaying Score 1 Answer
Changing the UI text value from server to Client side 0 Answers
adjustable point limit 1 Answer
How do I stop adding to a variable? 1 Answer