How to destroy objects of passed level?

I trying to load scenes with LoadLevelAdditiveAsync to create continuous world, but i need to destroy objects of passed scenes… And i dont imagine how can i do that :slight_smile: What you can suggest?

Once you load a new scene, the objects in the old scene are disposed of, you can check this by using

Application.LoadLevel("CurrentLevel");

and the level will reset(GameObjects and all)

First scene - Second Scene - Third Scene - …

Now loaded only First scene, i need to add Second to it.

When player got end of Second Scene i will add Third, but First must be destroyed. How? :slight_smile:

2 Scenes must be loaded at same time. And oldest of it must be destroyed dynamically when third secene is loading.

I’m sorry, perhaps I’m misunderstanding the question, can you elaborate on what you’re trying to do, and more importantly. Why?

i need continouosly world, whith 2 scenes at same time in it. When 3th scene is loading 1st must be destroyed.

So it will chain of scenes 1-2-3-4-5-6-7-8… etc And only 2 of it will be at world at one moment.

For example, if player stay at end of 4 scene, 5 starts to load, 3 starts to destroy.

Make an Event, something like OnPlayerEnterNewLevel(int level) and subscribe objects to this event. Next step:

if (level - objectLevel >= 2)
	Destroy (gameObject);

Possibly with Destroy (gameObject, Random.Range(0f, 5f)) to not to destroy all object at once and generate lag.

Load each scene into a single parent GameObject for that scene. When you need to unload a scene, just destroy the parent GameObject. To make it easier, you can organize all of the scenes under a master GameObject, such as:

[Scene Manager]
    [Scene 1]
        {scene 1 contents}
    [Scene 2]
        {scene 2 contents}
    ...

You can put a script on the Scene Manager object that maintains a list of which scenes are loaded, with functions to load and unload scenes.

When you build each individual scene, put everything in a GameObject named Scene N (where N is the scene number).

When Scene Manager loads the scene, it should find the Scene N GameObject, child it under the Scene Manager GameObject, and add it to the script’s list.

Unloading unused assets is a different topic. You could just let Unity handle this, or call Resources.UnloadUnusedAssets() at non-time-critical times.

oh thank you :slight_smile: i totally forget about scene is object too :slight_smile:

Interesting bit of information here.

I thought I’d put together some code that demonstrates what I was writing about. You can download the attached package and watch a quick video on how it works:

Alternate download link: Scene Streamer - Pixel Crushers

1539580–89477–$SceneLoader_1_0.unitypackage (88.2 KB)

Just wanted to chime in and say that what TonyLi is recommending is, AFAIK, the absolute best way to handle data that comes in from LoadLevelAdditive. I use a similar set of techniques in SECTR STREAM, just with some more complex asset tracking to handle lightmaps and more complex scenes, but the core idea is the same.

You do need to be careful that if you use LoadLevelAdditive (async or otherwise), Unity will usually not actually create the game objects until one or more frames after you call it, so you want to make sure you correctly handle that latency.