From what I see, my scripts are identical. But when I hit spikes on the first level, the player dies and nothing resets.
I noticed that when I go to the play mode, I don’t see gamesessions showing on the left hand side like it does for Rick in the Hierarchy.
My scripts.
GameSession.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameSession : MonoBehaviour
{
[SerializeField] int playerLives = 3;
void Awake()
{
int numGameSessions = FindObjectsOfType<GameSession>().Length;
if (numGameSessions > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
public void ProcessPlayerDeath()
{
if (playerLives > 1)
{
TakeLife();
}
else
{
ResetGameSession();
}
}
void TakeLife()
{
playerLives--;
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
}
void ResetGameSession()
{
SceneManager.LoadScene(0);
Destroy(gameObject);
}
}
PlayerMovement.cs (change only):
void Die()
{
if (myBodyCollider.IsTouchingLayers(LayerMask.GetMask("Enemies", "Hazards")))
{
isAlive = false;
myAnimator.SetTrigger("Dying");
myRigidbody.velocity = deathKick;
FindObjectOfType<GameSession>().ProcessPlayerDeath();
}
}
}