- Home /
OnSceneChange for Don'tDestroyOnLoad GameObjects?
Hi there Unity sages, I'm a student programmer that's currently stuck in a very weird situation.
- Don'tDestroyOnLoad
- Whenever specific scenes load(e.x Main) it's supposed to make references to gameobjects in the main scene from DDOL
- Use the functions from those Gameobjects.
What I have currently done is made a preloader Scene to instantiate my Managers to hold the references and made a script for it to get references from the main scene when the scene is loaded. I achieved that by using OnEnable and OnDisable and then doing SceneContainer.GetComponentsInChildren(true); and then sorting it all out. It was working however out of the blue it just started freaking out and stop working. The OnEnable/Disable is now being randomly called(sometimes not even being called) and on first run OnEnable/Disable happen before OnAwake in the scene. I have been frustrated because of this problem for 2 days now and i've run out of ideas. Sorry for the long read! Thanks for the help!
Notice how SceneLoadCheck is called before Awake does.
/// <summary>
///
/// Scene Swap Interaction
///
/// Resets the required References whenever the scene changes.
///
///
/// </summary>
private void OnEnable()
{
SceneManager.sceneLoaded += SceneHasLoaded;
}
private void OnDisable()
{
SceneManager.sceneLoaded -= SceneHasLoaded;
}
void SceneHasLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("Scene Load Check");
Debug.Log(SceneManager.GetActiveScene().name);
if (SceneManager.GetActiveScene().name == "_PreLoad")
{
SceneManager.SetActiveScene(SceneManager.GetSceneByName("Pre-Alpha 2.1.0"));
}
StartCoroutine(StartUp(scene));
}
The Code in question. StartUp(scene); is where I reference my objects
EDIT: I've been running more tests and it seems that my Main scene is being loaded more than once? I did SceneManager.sceneCount and it returned 4 active scenes when it should only be 2. I don't know if this might be relevant or not but it's the only result i've had in 5 hrs
Your answer
Follow this Question
Related Questions
Attempting to Create an Essential SceneElements Checker 0 Answers
Changing Scene Crashes Unity while in play mode 0 Answers
Scene Loading takes over 3 minutes 1 Answer
Scene loading problem 5 Answers