In first loop
When I start the ‘Game’ scene the score is displayed correctly then in ‘Game Over Screen’ scene also it is working fine.
But in second loop
when I click on ‘Play Again’ button then in ‘Game’ scene when I kill an enemy the score is displayed as 0 then in ‘Game Over Screen’ scene it is displayed correctly.
Scripts:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScoreKeeper : MonoBehaviour
{
int score = 0;
static ScoreKeeper instance;
void Awake()
{
if(instance != null)
{
gameObject.SetActive(false);
Destroy(gameObject);
}
else
{
instance = this;
DontDestroyOnLoad(gameObject);
}
}
public void SetScore(int enemyPoints)
{
score += enemyPoints;
}
public int GetScore()
{
return score;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class UIDisplay : MonoBehaviour
{
[SerializeField] TextMeshProUGUI scoreText;
[SerializeField] Slider healthSlider;
ScoreKeeper scoreKeeper;
void Awake()
{
scoreKeeper = FindObjectOfType<ScoreKeeper>();
}
void Update()
{
scoreText.text = scoreKeeper.GetScore().ToString("000000000");
}
public void SetSliderValue(int healthPoints, int initialHealthPoints)
{
healthSlider.value = (float)healthPoints / initialHealthPoints;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class GameOverUIDisplay : MonoBehaviour
{
[SerializeField] TextMeshProUGUI scoreText;
ScoreKeeper scoreKeeper;
void Awake()
{
scoreKeeper = FindObjectOfType<ScoreKeeper>();
}
void Start()
{
scoreText.text = scoreKeeper.GetScore().ToString("000000000");
}
}