On scene change event for DontDestroyOnLoad object?

Hi. I have a script that manages other scripts for my Player object.

The Player is DontDestroyOnLoad and i can’t figure out how to call a function on any scene change.

My code below:

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

public class ScriptSceneManager : MonoBehaviour
{
    private PlayerMovement movement;
    private PlayerBlock blockPlacing;
    private Attacks attacking;
    private GhostBlockManager ghostBlock;
    private PlayerHotBar hotBar;
  
    void Awake(){

        movement = GetComponent<PlayerMovement>();
        blockPlacing = GetComponent<PlayerBlock>();
        attacking = GetComponent<Attacks>();
        ghostBlock = GetComponent<GhostBlockManager>();
        hotBar = GetComponent<PlayerHotBar>();
        DontDestroyOnLoad(gameObject);
    }

    void Start(){
        SceneManager.sceneLoaded += OnSceneLoaded;
    }

    void EnableFunctions(){
        movement.enabled = true;
        movement.reFindReferences();
        blockPlacing.enabled = true;
        blockPlacing.reFindReferences();
        attacking.enabled = true;
        hotBar.enabled = true;
        ghostBlock.enabled = true;
    }

    public void DisableFunctions(){
        blockPlacing.enabled = false;
        attacking.enabled = false;
        ghostBlock.enabled = false;
        hotBar.enabled = false;
    }

  

    void OnEnable()
    {
        SceneManager.sceneLoaded += OnSceneLoaded;
        print("Scene has loaded");
    }

    void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {

        print("Scene on scene");

        if (scene.name != "Testing" && scene.name != "Battle"){
            DisableFunctions();
        }

        else if(scene.name == "Battle"){
            print("is battle");
            EnableFunctions();
            Invoke("SetBattleSpawnPos", 0.2f);
            SetBattleSpawnPos();
        }
        else if(scene.name == "Testing"){
            EnableFunctions();
        }
    }

    private void SetBattleSpawnPos(){

        LevelGenerator gen = LevelGenerator.instance;

        int xSpawnPos = gen.surfacemaxX/2;
        Vector2 spawnPos = gen.getSurfacePos(xSpawnPos);
        spawnPos.y += 1;
        transform.position = spawnPos;
    }
}

When changing scenes, OnSceneLoaded is never called. OnEnabled is only called once (when the object is created). How do i trigger OnSceneLoaded when a scene is loaded when my object has DontDestroyOnLoad? I want to be able to disabled and enable different functions based on which scene is loaded. Thanks.

Order of operations: OnEnable → Awake → Start. They will only be called once, even when a new scene is loaded for objects with DontDestroyOnLoad. OnEnable will be called if you disable and re enable the object after the object is first enabled on creation.

I use SceneManager.sceneLoaded += OnSceneLoaded; all the time in Awake or OnEnable and it works fine. I usually have it on a script that is run before everything else(script execution order.)

It should work. Make sure its only set in OnEnable or Awake and try setting the script execution order of that script before the default time. I stick all of my Manager scripts before default time to ensure that they are loaded and ran before everything else.

4 Likes

Sorry if i wasn’t clear, I need a function that is called on EVERY scene load, not just once.

IE. whenever a scene is loaded, i want the script to do something based on which scene is loaded.

That’s how it works. Once you register the event it will trigger on EVERY scene load. If you tried my suggestion and it still doesn’t work then you found a bug.

1 Like