Why does ScenePersist not work and why is this OK?

Hi, as I finished this video and saw the code that Rick had us write, I thought to myself “how could this work?!” I then tried it and guess what, it DOESN’T. I am frustrated that only two people posed a question as to why this doesn’t work and frustrated that I worked on it myself for a while before coming here thinking I did something wrong. (I even watched the video twice all the way through to make sure I followed along correctly).

Is it Unity being a newer version that causes this to no longer work, or did Rick just teach it without testing it out himself? This just seems crazy to teach people how to do something and what you teach isn’t even correct. This isn’t a question of good practice or style, the code as instructed doesn’t even work!

When you load the second level, the singleton in this code destroys the ScenePersist gameobject loaded with it , causing no pickups to spawn. Please edit the video with a solution so people can actually receive proper instruction.

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

public class ScenePersist : MonoBehaviour
{
    int startingSceneIndex;

    private void Awake()
    {
        int numScenePersist = FindObjectsOfType<ScenePersist>().Length;
        if (numScenePersist > 1)
        {
            Destroy(gameObject);
        }
        else    
        {
            DontDestroyOnLoad(gameObject);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
       startingSceneIndex = SceneManager.GetActiveScene().buildIndex;
    }

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

Olivier posted a working solution in another thread. I was just frustrated with a single video. Love the course but this video was disappointing…

Solution:

int startingSceneIndex = -1;

    public int GetStartingSceneIndex ()
    {
        return startingSceneIndex;
    }

    void Awake ()
    {
        startingSceneIndex = SceneManager.GetActiveScene ().buildIndex;
        ScenePersist[] scenePersists = FindObjectsOfType<ScenePersist> ();
        if (scenePersists.Length <= 1) {
            // I am alone
            DontDestroyOnLoad (gameObject);
        }
        else {
            bool destroyMe = false;
            // We are Two
            foreach (ScenePersist scenePersist in scenePersists) {
                if (scenePersist != this) {
                    // It's not me
                    if (scenePersist.GetStartingSceneIndex () != startingSceneIndex) {
                        // You have nothing to do here
                        Destroy (scenePersist.gameObject);
                    }
                    else {
                        // I have nothing to do here
                        destroyMe = true;
                    }
                }
            }

            if (destroyMe) {
                // Seppuku!
                Destroy (gameObject);
            }
            else {
                // They are all dead, I will survive!
                DontDestroyOnLoad (gameObject);
            }
        }
    }

    void Update ()
    {
        int currentSceneIndex = SceneManager.GetActiveScene ().buildIndex;
        if (currentSceneIndex != startingSceneIndex) {
            DestroyObject (gameObject);
        }
    }
1 Like

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

Privacy & Terms