Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Noztradamuz · Jun 06, 2013 at 08:26 PM · c#sceneloadlevel

How clean the level before loading another one?

Hi, I have a problem, I'm making a game where the level is generated randomly whenever the player plays a new game (like Diablo II), when the player dies the application loads another level in order to the user to pick a new character then bring the player to accion again loading the previous level again, now, I want to do this... I do managed the problem of the variables and things, however. The "scenery" is not only generated randomly it will also appears and disappears when the player gets closer or away, just like the Render distance in most games, this is for optimizing Mobile devices. The problem is. the level is not cleaned when it's loaded for second time, so is not even generated again, but also the loaded prefabs(entities) remain. I can delete the "map" with just fill a GameObject array with FindGameObjectsWithTag("MapBlock") for example and do Destroy() into a For loop but how can I properly delete also the small entities like Enemies, powerups, traps, etc. There are a lot of them, it could be so much work for mobile devices to do a massive Destroy() loop. I want to clean the whole scene when its Reloaded, or clean it before load another level, but leaving maybe one object in the scene in order to execute the scrip to re-generate the map and respawn entities.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TonyLi · Jun 07, 2013 at 01:39 AM

Make all of those objects children of a single root object. Let's say you call the object "Level". To clear the scene, just destroy Level. To generate a new scene, add the generated objects as children of Level. Other objects (say one is called NeverUnloadMe) won't be affected.

For example, your scene hierarchy could look like this:

 NeverUnloadMe
 Level
     Scenery
         map block 1
         map block 2, ...
     Dynamic
         enemy 1
         enemy 2, ...
         projectile 1, ...
         powerup 1, ...


This way you don't have to deal with LoadLevel().

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Noztradamuz · Jun 07, 2013 at 03:02 PM 0
Share

Thanks I was thinking on something like that, maybe not make them all childrens of one big daddy but make every entity son of his "Scenery block" father, then delete only the map blocks at the end. and that should delete entities too.

avatar image Noztradamuz · Jun 07, 2013 at 03:07 PM 0
Share

And unfortunately I always need to deal with LoadLevel thing because of the variables and stuff that for some reason don't get executed on the Start function of the script the second time I ran the level

avatar image
0

Answer by Dave-Carlile · Jun 06, 2013 at 08:56 PM

Create an empty level, and do a LoadLevel on that before generating your scene. So your menu is one scene, and the game is the "empty" scene.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Noztradamuz · Jun 06, 2013 at 09:32 PM 0
Share

I don't understand, I have two Scenes one with the menu and one with the "game" where I'm generating the scenery and everything, I have one Script that generates the scenery with random "scenery blocks" and every "scenery block" have their own entity spawner, also random spawned (I mean some of them spawn enemies, some traps, some powerups and things like that). I can succesfully restart the Level from the level itself, and everything works like charm, but when I try to load (re-load actually) the level from the menu, everything mess up!. How I will manage to get an Empty scene everytime i want to recreate the scenery and the entities, isnt that what I just recently asked?

avatar image Dave-Carlile · Jun 06, 2013 at 10:09 PM 0
Share

The empty scene is part of your project. Each time you do a LoadLevel for that scene it will load the empty scene. It will start of empty, and you can then generate the game objects.

avatar image
0

Answer by Noztradamuz · Jun 06, 2013 at 10:46 PM

Ok I managed to do this by doing this:

I moved every code line i was executing on the Start funcion on almost every script related to the Level, into a Function for example: Initialize() and i call that function into the Start() and OnLevelWasLoaded() functions, also i'm destroying the "scenery blocks" if there are any in the OnLevelWasLoaded() and Start() functs. then respawn them all again, I hope there's no junk left on the scene like missed entities or something that can make the game slower and slower the more they play.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Click handling at world scene level 1 Answer

How to save a Scene during a game? 1 Answer

Multiple Cars not working 1 Answer

Can't get simple script to work? 4 Answers

How Do You Have Multiple High Scores For 1 GameOverScene? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges