![]() iterate through the objects with this component, deleting those with matching identifiersįoreach (DontDestroyOnSelectedScenes obj in collection) cache all objects containing this componentÄontDestroyOnSelectedScenes collection = FindObjectsOfType() check if this object should be deleted based on the input scene names given ![]() ![]() delete any potential duplicates that might be in the scene already, keeping only this one Void OnSceneLoaded(Scene scene, LoadSceneMode mode) SceneManager.sceneLoaded += OnSceneLoaded since Awake() callback preceded OnSceneLoaded(), place initialization code in Start() for singleton-like behaviour: we need the first object created to check for other objects and delete them in the scene during a transition Public class DontDestroyOnSelectedScenes : MonoBehaviour ![]() / For reusability, this component uses the scene names as strings to decide whether it survives or not after a scene is loaded theme songs) in certain scene transitions. / Attach this component to objects that you want to keep alive (e.g. Here is a working solution that avoids the use of singletons, allowing you to add the script to multiple objects in the scene and tweak the behaviour via the inspector: using First thread I found when looking for a solution to this problem, so I'll add to this thread.
0 Comments
Leave a Reply. |