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