How to detect when scene has changed in a DontDestroyOnLoad script?

Good day.

I have a script with DontDestroyOnLoad property. I need to run one method when one scene in particular is loaded. Its clear i need to check for the activeScene , but I don’t know how to trigger code when a scene is loaded. (It seems OnLevelWasLoaded does not work anymore…).

I don’t get how to do it with:

Some help please?

To detect the scene I know is:

using UnityEngine.SceneManagement;

if( SceneManager.GetActiveScene().name=="Something")
{
    RunMyFunctions();
}

Where I need to put this to make it run only when a scene is loaded?

Thaanks!

You could try using SceneManager listen to sceneLoaded event.

You can use SceneManager.activeSceneChanged event.

Example:

void Awake() 
{
    SceneManager.activeSceneChanged += OnSceneChange;
}

private void OnSceneChange(Scene current, Scene next)
{
    YourMethod();
}

private void YourMethod()
{
    // does something
}