- Home /
How can I continue the game without losing values after Game Over?
Hello How can I continue the game without losing values after Game Over? Let's say Game Over happened and my Player object disappeared from the scene and the player will be able to continue the game by watching an ad. How can I continue the game without losing values such as gold and score? I save my values with playerPrefs. I write this but all values are zero. I guess these games also have an update because new scene has been uploaded.
public void watchRewardedAdResumeGame(object sender, Reward args)
{
SceneManager.LoadScene("Level");
PlayerPrefs.SetInt("Gold",PlayerPrefs.GetInt("Gold") + goldResume);
goldResume=PlayerPrefs.GetInt("Gold");
goldText.text="Gold: " + goldResume;
PlayerPrefs.SetFloat("score",PlayerPrefs.GetFloat("score") + scoreResume);
scoreResume=PlayerPrefs.GetFloat("score");
scoreText.text=((int)scoreResume).ToString();
}
Make a new script and delete the monobehaviour component. Then make all your variables static so that you can access them from other scripts. Then when game over you update the variables on the non MonoBehaviour script and when you load the scene again you load the variables onto the score script. It's basically a save and load script
would make more sense to make make the class static instead of all the variables.. but a scriptable object can do all that and persists between scenes.
There might be two problems: 1- You first load the scene, then set playerprefs. So your values may be reset. 2-If there is no other script that changes PlayerPrefs.GetInt("Gold"), this script only makes it zero because you did not define goldResume and scoreResume before.
Answer by SoulBlaze06 · Jul 28, 2021 at 09:51 AM
Are you using a different scene for GameOver. If so, then you should not use it and instead you should create a UI Canvas and then an empty gameobject named "game over" and under that gameobject you can parent all your buttons, images and gameover text etc. After that you can simply enable and disable the gameobject so that it only appears when player has lost or simply gameover. In this way you will not have to switch scenes and will also save you time.
public GameObject gameOverMenu; // Creating a gameObject private bool isGameOver; if(isGameOver = true) gameOverMenu.SetActive(true); // if you want to enable the object Time.timeScale = 0; // Just so that your game gets paused and make sure to make it 1 after you want your gameOverMenu To be disabled
else if(isGameOver = false) gameOverMenu.SetActive(false); // if you want to disable the object Time.timeScale = 1; // basically resuming the game so that it is no longer a time freeze
Your answer
Follow this Question
Related Questions
Saving character selection 1 Answer
PlayerPrefs for FPS? 1 Answer
Save/Load Variable with playerprefs 1 Answer
Set int to object from list? 0 Answers