OnSceneExit?

Hello, how can I do something when the player leaves the scene? Just like the OnApplicationQuit :slight_smile:

Thanks, Andreas.

There is OnDestroy() which might work, because all objects are destroyed when you leave a scene.

You can write a function with the same signature as UnityAction, and add it to the sceneUnloaded event. Hereโ€™s an example:

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;

public class MyClass : MonoBehaviour {
	void Awake () {
		SceneManager.sceneUnloaded += MyFunction;
	}

	void MyFunction<Scene> (Scene scene) {
		print ("The scene was unloaded!");
	}
}

You can add several functions to the sceneUnloaded event, and they will all be executed when the scene is unloaded.

I wish this existed but it does not.