How to correctly dispose scenes?

Do I destroy a scene like I destroy gameobjects in TS? Because my scene Is gone when I call DeactivateScene() but loadedSceneInstance still has data in it. Do I just manually set it null or is there further cleanup necessary?

// @type UnityEditor.SceneAsset
@serializable(AssetReference)
scene: AssetReference | null = null;

loadedSceneInstance? : Object3D;

public async ActivateScene() {
    if (!this.scene?.isLoaded()) {
        await this.scene.loadAssetAsync();
    if (this.loadedSceneInstance) {
        this.loadedSceneInstance = await this.scene.instantiate();
    }
}
public async DeactivateScene() {
    if (this.loadedSceneInstance != null)
        GameObject.destroy(this.loadedSceneInstance, true, true);
    if (this.scene.isLoaded()) {
        await this.scene.unload();
    }
}

Original Post on Discord

by user 334342083445784576

Also, I have this weird problem, in which all Object3D.visible or GameObject.SetActive calls are not getting through until I instantiate the scene a second time? What’s happening here? :smile:

by user 334342083445784576

okay, figured it out. Scenes are just added and not instantiated :).

by user 334342083445784576

Hey, generally in three disposal is manual / you decide what you dispose:
https://threejs.org/docs/#manual/en/introduction/How-to-dispose-of-objects

That being said, calling GameObject.destroy with the third parameter (“dispose”) set to true - as you do up there - goes through the hierarchy of that object and disposes resources
(will have to double check regarding textures, but meshes are disposed)

Did you have to adjust your code above for your usecase or is that already the adjusted / final working version?