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