Problem with scene loader instance

Hello everyone. I get this very lame problem when dying in the third scene. I don’t get why this is happening. in this scene there is a sceneloader script attached to the gameobject and I also checked the code if the session script instantiate a sceneloader for that purpose.

here is my code for that:

int gameSessionNum;
static int actualPlayerLives;
static int playerScore = 0;
SceneLoader sceneLoader;

void Awake()
{
    gameSessionNum = FindObjectsOfType<GameSession>().Length;
    if (gameSessionNum > 1)
    {
        Destroy(gameObject);
    }
    else
    {
        DontDestroyOnLoad(gameObject);
    }
}

void Start()
{
    sceneLoader = FindObjectOfType<SceneLoader>();
    actualPlayerLives = playerMaxLives;
}

public void ProcessPlayerDeath()
{
    if (actualPlayerLives <= 0)
    {
        actualPlayerLives = playerMaxLives;
        sceneLoader.LoadStartScene();
    }
    else
    {
        actualPlayerLives--;
        sceneLoader.ReloadScene();
    }
}

Thanks for helping me :slight_smile:

Is this the GameSession class? If so, I believe the Start method is only called once while the awake method is called each time the player dies. Have you tried moving “sceneLoader = FindObjectOfType();” into the awake method? I believe you can also use the OnLevelWasLoaded method for this.

Privacy & Terms