OnSceneLoad and DontDestroyOnLoad combination

Im having a little difficulty here with OnSceneLoad. it appears to be way harder to use than is necessary, unless i’m missing something, in the way i need to use it.

i’m trying something fairly simple here, that way i can make sure things fire properly, but the onsceneload method seems to take an argument that i can’t provide how i need it to, and that’s the scene name.

apparently, you can’t get the name of a scene unless it’s loaded, but i need the scene name of the upcoming scene, so i know when it loads lol.

let me give the simple example.

void Awake()
{
    //do things like creating a prefab, assigning it's references, etc.
}

void Start()
{
    DontDestroyOnLoad(this);
}

void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
    //pretty sure i actually want all the stuff in Awake here, possibly the
    //dont destroy part too
}

public void SceneForward()
{
    sceneToLoad++;
    if (sceneToLoad > 5) { sceneToLoad = 5; }
    text.text = sceneToLoad.ToString();
    SceneManager.LoadScene(sceneToLoad);
    OnSceneLoaded(sceneToLoad, LoadSceneMode.Single);
}

public void SceneBack()
{
    sceneToLoad--;
    if (sceneToLoad < 0) { sceneToLoad = 0; }
    text.text = sceneToLoad.ToString();
    SceneManager.LoadScene(sceneToLoad);
    OnSceneLoaded(sceneToLoad, LoadSceneMode.Single);
}

very simple way of moving back and forth in the build index. problem is, awake doesn’t fire on a new scene loading, even though the AI generated first answer in google says it does, which is interesting, considering everything else says it doesn’t. that means i need the onsceneload thing, but it takes a name instead of an index. can’t get the name until the scene is loaded, and i’m unsure if it will either fire where it is in those two methods or not, and can’t ge tthe name, so…

is there a way to use the index instead of the name that nothing has mentioned, or is there a simple way to get the name? or if i want to do it like this, do i have to rename all of my scenes to 0, 1, 2, 3, etc?

You need to subscribe the method to SceneManager.sceneLoaded.

That’s shown clearly in the docs: Unity - Scripting API: SceneManagement.SceneManager.sceneLoaded

which i assume is something that is done through OnEnable or Awake, which is the issue here, since those don’t happen again. that particular example there seems like a one off to get a single object to do a thing on scene load or unload, not an object that carries over to each scene.

also distinctly possible that i’m not understanding if onsceneloaded being subscribed to like that also carries over, and just fires on its own…

You don’t need it to happen more than once. A DontDestroyOnLoad game object would subscribe in Awake or whatever, and thus would remain subscribed throughout its lifetime, receiving a callback every time a scene is loaded.

That is the point of subscribing to a delegate, you are the listener simply doing something when the publisher broadcasts that something has happened.

yep, just figured it out. the “SceneManager.sceneLoaded” part must have passed through the part of my brain that contains logic in the wrong way lol

now i get to figure out why my little button prefab is showing up in the wrong place after the first scene! :smiley: