I have a big problem and I was just looking for a simple answer. I want to reset the player statistics to default (Score to zero and Lives to 9 for example) after winning the game, going back to the title scene and then to the first level again.
How do I do that? Here is the relevant code:
GameStatus:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameStatus : MonoBehaviour
{
static public int lives = 9;
static public int score = 0;
public AudioSource source;
public AudioClip bell;
public static int target = 100;
public PlayerController ball;
public HealthManager hm;
public GameObject gameOverScreen;
public GameObject currentCheckpoint;
public CountdownTimer ct;
// Start is called before the first frame update
void Start()
{
source = GetComponent<AudioSource>();
gameOverScreen.SetActive(false);
ball = FindObjectOfType<PlayerController>();
hm = FindObjectOfType<HealthManager>();
ct = FindObjectOfType<CountdownTimer>();
}
// Update is called once per frame
void Update()
{
if (lives <= 0)
{
gameOverScreen.SetActive(true);
ball.gameObject.SetActive(false);
}
}
public void AddScore(int s)
{
score += s;
while (score >= target)
{
AddLives(1);
target += 100;
source.PlayOneShot(bell);
}
}
public void ScoreMultiply()
{
int multiScore = 10;
score += multiScore;
while (score >= target)
{
AddLives(1);
target += 100;
source.PlayOneShot(bell);
}
}
public void AddLives(int l)
{
lives += l;
}
public void LoseLives()
{
lives--;
}
void OnDestroy()
{
Debug.Log("'GameStatus' was destroyed");
}
public void Reboot()
{
score = 0;
lives = 9;
gameOverScreen.SetActive(false);
ball.gameObject.SetActive(true);
ct.ResetTime();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void Reset()
{
GameManager.instance.score = 0;
GameManager.instance.lives = 9;
}
}
And the problem lies here:
GameManager:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public bool isPaused;
public Text scoreText, livesText;
public int score = 0;
public int lives = 9;
void Awake()
{
if(instance == null)
{
instance = this;
}
else
{
if(instance != this)
{
Destroy(gameObject);
}
}
DontDestroyOnLoad(gameObject);
}
void Update()
{
scoreText.text = score.ToString();
livesText.text = lives.ToString();
}
}
Apparently, I want to keep the DontDestroyOnLoad method during gameplay, but also want to reset the game stats after the game is reset. Can anyone tell me how to do this? Thank you very much.
Sincerely,
burchland2