ScenePersist Issue (gameObject + script) - My solution experience

Hello colleagues! In this topic, I would like to share with you my ScenePersist issue solution experience!

What was the problem?
ScenePersist gameObject was working only at the 1st Level of my TileVania game:
If the player’s character collects the coins and dies, but has 1-2 lives left, then the player’s character appears at the starting point of the level again, has the score equal to collected coins, and there are no collected coins at the level.

I had in my Level 1 hierarchy ScenePersist gameObject (with ScenePersist.cs on it), and Pickups empty gameObject attached under it. Pickups gameObject have the Coins prefabs under itself.
image

It seemed to me logical to make a Scene Persist prefab for its further use on other scenes. Prefab has nothing but Transform and ScenePersist.cs components. No Pickups with it, no coins.

On my other levels, I used the ScenePersist prefab in the Hierarchy, placed Pickups and Coins under it. And I realized that it doesn’t work. //Now I think “Maybe I was tired and didn’t update my ScenePersist prefab, so it was without the script on other levels???”…

So, what I did:
image
Created in every each level unique EmptyGameObject, named it ScenePersist+№ attached script to it and saved as a prefab
For every ScenePersist prefab, I reset the Transform component

And at every scene I have this:
image
image

The script is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ScenePersist : MonoBehaviour
{

    int startingSceneIndex;

    private void Awake()
    {
        //startingSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int numScenePersist = FindObjectsOfType<ScenePersist>().Length;
        if (numScenePersist > 1)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
        }
    }

    // Use this for initialization
    void Start()
    {
        startingSceneIndex = SceneManager.GetActiveScene().buildIndex;
    }

    // Update is called once per frame
    void Update()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        if (currentSceneIndex != startingSceneIndex)
        {
            Destroy(gameObject);
        }
    }

    public void WipeScenePersistence()
    {
        gameObject.SetActive(false);
        Destroy(gameObject);
    }
}

WipeScenePersistence method called in two scripts:

In the GameSession.cs, in the ProcessPlayerDeath() method:

    public void ProcessPlayerDeath()
    {
        if (playerLives > 1)
        {
            //FindObjectOfType<ScenePersist>().WipeScenePersistence();
            TakeLife();
        }
        else
        {
            FindObjectOfType<ScenePersist>().WipeScenePersistence();
            ResetGameSession();
        }
    }

In the LevelExit.cs, in the OnTriggerEnter2D method:

    private void OnTriggerEnter2D(Collider2D collision)
    {
        FindObjectOfType<ScenePersist>().WipeScenePersistence();
        StartCoroutine(LoadNextLevel());
    }

All working nice with this configuration. My Unity version is 2020.3.0f1 Personal

3 Likes

Hi Artyom,

Thanks a lot for sharing your experience and your detailed solution! I’m sure this will be very helpful for future students! :slight_smile:

1 Like

Thanks for sharing your solution. It’s always great to see when fellow students share their own different methods to problems.

1 Like

@Nina @XtremeJam87
I hope my solution will be useful to someone. Thank you!
:pray:

Privacy & Terms