- Home /
Can't restart my game after game over
Hello Unity Community! I ran into some problems that i just can't figure out on my own i realized after hours of searching. So here is my problem
I have playerprefs keeping track on my health and score. after my health gets to 0 you get back to the startscene (main menu) and playerprefs get deleted ( i assume? ). now the problem is when i press play again in the menu, the game starts for a second and then i get back into the menu again. I think that the problem is my game thinks my health is still 0 so it returns me to the main menu, but i can't figure out how to do it. here is my code. atleast for my gamemaster script that handles it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class gameMaster : MonoBehaviour
{
public int score;
public Text scoreText;
public Text InputText;
public int curHealth;
public int maxHealth = 4;
private PlayerController player;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
Scene currentScene = SceneManager.GetActiveScene();
string sceneName = currentScene.name;
if (PlayerPrefs.HasKey("Score"))
{
if (sceneName == "StartScene")
{
PlayerPrefs.DeleteKey("Score");
score = 0;
}
else
{
score = PlayerPrefs.GetInt("Score");
}
}
if (PlayerPrefs.HasKey("Health"))
{
if (sceneName == "StartScene")
{
PlayerPrefs.DeleteKey("Health");
curHealth = 0;
}
else
{
curHealth = PlayerPrefs.GetInt("Health");
}
}
}
private void Update()
{
scoreText.text = ("X " + score);
if (curHealth > maxHealth)
{
curHealth = maxHealth;
}
if (curHealth <= 0)
{
Die();
}
if (score >= 100)
{
GiveHealth();
}
}
void GiveHealth()
{
if (curHealth == maxHealth)
{
score = 0;
}
else
{
score = 0;
curHealth += 1;
}
}
public void TakeDamage(int damage)
{
curHealth -= damage;
}
private void Die()
{
SceneManager.LoadScene("StartScene");
}
}
now i've tried alot of different things to change this. for example i tried in my Die method to give health a new value after the startscene is loaded. i tried searching if i could make a new player pref after the old one was deleted with a new value so it doesn't stay at 0 so i can't start my game, but i have no idea how to solve this. can anyone help me?
cheers
A few ideas of the top of my head.
You can put a Debug.Log message in your Die() method to check whether it is called once you click on play in the main menu. You can also put a debug.Log message in your PlayerPrefs.DeleteKey if statement to check whether is actually called. Also put a Debug.Log message in start to check what the actual sceneName is. $$anonymous$$aybe you have some hiccup with the active scene and StartScene is never the active one.
Is your game$$anonymous$$anager a prefab instance that you put in your game scene and your main menu? Because if you have it in your game scene only and it doesn't exist in the main menu (or StartScene) then the PlayerPrefs deletion will obviously never happen.
Oh and most importantly. Once you are deleting the PlayerPrefs.Health key in StartScene you then make Health = 0. That means your health is now 0. That means when you load the game scene your health is 0 and you die. So rh_galaxy suggests the solution here.
Answer by rh_galaxy · Sep 14, 2020 at 07:04 AM
Something like this, but I can't see where you set PlayerPrefs or where you load other scenes than the StartScene:
public class gameMaster : MonoBehaviour
{
public int score;
public Text scoreText;
public Text InputText;
public int curHealth;
public int maxHealth = 4;
private PlayerController player;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
Scene currentScene = SceneManager.GetActiveScene();
string sceneName = currentScene.name;
if (sceneName == "StartScene")
{
PlayerPrefs.DeleteKey("Health");
curHealth = 0;
PlayerPrefs.DeleteKey("Score");
score = 0;
}
else
{
if (PlayerPrefs.HasKey("Health")) curHealth = PlayerPrefs.GetInt("Health");
else curHealth = maxHealth;
if (PlayerPrefs.HasKey("Score")) score = PlayerPrefs.GetInt("Score");
else score = 0;
}
}
private void Update()
{
scoreText.text = ("X " + score);
if (curHealth > maxHealth)
{
curHealth = maxHealth;
}
if (curHealth <= 0)
{
Die();
}
if (score >= 100)
{
GiveHealth();
}
}
void GiveHealth()
{
score = 0;
if (curHealth < maxHealth)
{
curHealth += 1;
}
}
public void TakeDamage(int damage)
{
curHealth -= damage;
}
private void Die()
{
SceneManager.LoadScene("StartScene");
}
}