Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How to destroy objects of passed level?

Discussion in 'Scripting' started by aradriel, Feb 28, 2014.

  1. aradriel

    aradriel

    Joined:
    Nov 28, 2011
    Posts:
    47
    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 :) What you can suggest?
     
    Last edited: Feb 28, 2014
  2. Christian-Tucker

    Christian-Tucker

    Joined:
    Aug 18, 2013
    Posts:
    376
    Once you load a new scene, the objects in the old scene are disposed of, you can check this by using

    Code (csharp):
    1. Application.LoadLevel("CurrentLevel");
    and the level will reset(GameObjects and all)
     
  3. aradriel

    aradriel

    Joined:
    Nov 28, 2011
    Posts:
    47
    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? :)

    2 Scenes must be loaded at same time. And oldest of it must be destroyed dynamically when third secene is loading.
     
    Last edited: Feb 28, 2014
  4. Christian-Tucker

    Christian-Tucker

    Joined:
    Aug 18, 2013
    Posts:
    376
    I'm sorry, perhaps I'm misunderstanding the question, can you elaborate on what you're trying to do, and more importantly. Why?
     
  5. aradriel

    aradriel

    Joined:
    Nov 28, 2011
    Posts:
    47
    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.
     
  6. DormanSh

    DormanSh

    Joined:
    Sep 11, 2012
    Posts:
    31
    Make an Event, something like OnPlayerEnterNewLevel(int level) and subscribe objects to this event. Next step:
    Code (csharp):
    1. if (level - objectLevel >= 2)
    2.     Destroy (gameObject);
    Possibly with Destroy (gameObject, Random.Range(0f, 5f)) to not to destroy all object at once and generate lag.
     
  7. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,712
    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:
    Code (csharp):
    1.  
    2. [Scene Manager]
    3.     [Scene 1]
    4.         {scene 1 contents}
    5.     [Scene 2]
    6.         {scene 2 contents}
    7.     ...
    8.  
    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.
     
  8. aradriel

    aradriel

    Joined:
    Nov 28, 2011
    Posts:
    47
    oh thank you :) i totally forget about scene is object too :)
     
  9. Christian-Tucker

    Christian-Tucker

    Joined:
    Aug 18, 2013
    Posts:
    376

    Interesting bit of information here.
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,712
  11. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    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.