Game session script not working

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();
    }
}

}

Hi,

‘Nothing happens’ is a bit vague because we cannot see that code gets executed. ‘Nothing happens’ is rarely true. Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Thanks for getting back to me. The nice people on discord helped me diagnose the issue.

I somehow had the script added twice to my Game Session game object, and that was causing the issues.

Happy New Year! :slight_smile:

I’m glad you managed to solve the problem. Inadvertendly adding a script twice is a common mistake. It happened to me, too.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms