At the second and third level, the Player doesn’t lose lives. At the Inspector, for GameSession, I can see Player Lives reducing from 3 to 2 at the first level, but at the second, after death, the Player Lives is still 2. I added a debug at the ProcessPlayerDeath() and TakeLife() methods. The Player Lives always looks to reset to 3, after death.
YouTube - Example
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class GameSession : MonoBehaviour
{
[SerializeField] int playerLives = 3;
[SerializeField] int score = 0;
[SerializeField] TextMeshProUGUI livesText;
[SerializeField] TextMeshProUGUI scoreText;
void Awake()
{
int numGameSessions = FindObjectsOfType<GameSession>().Length;
if (numGameSessions > 1)
{
Destroy(gameObject);
gameObject.SetActive(false);
}
else
{
DontDestroyOnLoad(gameObject);
}
Debug.Log("PlayerLives Awake: " + playerLives);
}
private void Start()
{
livesText.text = playerLives.ToString();
scoreText.text = score.ToString();
}
public void ProcessPlayerDeath()
{
if (playerLives > 1)
{
Debug.Log("PlayerLives 0: " + playerLives);
TakeLife();
}
else
{
ResetGameSession();
}
}
public void AddToScore(int pointsToAdd)
{
score += pointsToAdd;
scoreText.text = score.ToString();
}
void TakeLife()
{
Debug.Log("PlayerLives 1: " + playerLives);
playerLives--;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
livesText.text = playerLives.ToString();
Debug.Log("PlayerLives 2: " + playerLives);
}
void ResetGameSession()
{
FindObjectOfType<ScenePersist>().ResetScenePersist();
SceneManager.LoadScene(0);
Destroy(gameObject);
gameObject.SetActive(false);
}
}