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 Vesuvian · May 15, 2012 at 10:54 PM · gamedestroyrestart

World hierarchy doesn't finish destroying before starting a new world?

Hi all

This is an awkward one to explain concisely.

Basically, I have a "restart the game" function that looks like this:

 function startNewGame(){
     // If there is a game in progress, end it
     var currentGame = gameObject.GetComponent(gameScript);
     Destroy(currentGame);
 
     // Start a new game
     gameObject.AddComponent(gameScript);
 }

gameScript's Destroy function goes on to Destroy() the world, the players, etc and those in turn Destroy() their own children.

Now, I think what I'm seeing is this: the game hasn't finished clearing itself up before the new game is started. I'm getting artefacts like players falling through the level on the 2+ time the level is created, because it would seem the first and subsequent games haven't been destroyed properly.

One thing I have tried is making it so after the first game everything is destroyed but the second level is not created. In the inspector I then trigger the creation of the next level. This is made flawlessly!

I'm really at a loss of what to do now. Is my method of cleaning up the game completely wrong? How can I ensure everything is destroyed before moving on to build the next level?

Any advice is massively appreciated.

Thanks,

Vesuvian

Comment
Add comment · Show 1
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 aldonaletto · May 16, 2012 at 02:46 AM 1
Share

Why don't you use a simple Application.LoadLevel? It destroys everything in the level and creates all objects of the new level. If you want to reload a level, use Applcation.LoadLevel(Application.loadedLevel)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by rutter · May 16, 2012 at 05:22 AM

You might want to check out the official script reference page for Destroy(), which I will quote in part:

Actual object destruction is always delayed until after the current Update loop

Like aldonaletto says, you might try just loading (or reloading) a level.

If you really want to do things this way, you could do something that waits one frame, or otherwise makes sure that the object(s) in question have been destroyed.

You might try DestroyImmediate(), but the script reference doesn't recommend it.

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 Vesuvian · May 20, 2012 at 10:44 PM 1
Share

Thank you very much, I can't believe how often I read the Destroy reference page without that sinking in.

avatar image Bunny83 · May 20, 2012 at 11:13 PM 0
Share

Don't use DestroyImmediate. It's mainly ment for editorscripts. You can't use Destroy in the editor since there are no "frames" in the editor the Destroy could wait for.

Loading levels is the simplest way. Another one is to use a coroutine and wait 1 frame ;)

 function startNewGame(){
     var currentGame = gameObject.GetComponent(gameScript);
     Destroy(currentGame);
     
     yield;
     
     gameObject.AddComponent(gameScript);
 }

Just as a hint: Class names usually should start with a capital letter so they aren't mistaken with variable names. It's a basic convention which is used by almost all program$$anonymous$$g styles out there. Sure, it doesn't matter when you're the only one that works on this project, but at least now we are involved as well ;)

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

6 People are following this question.

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

Related Questions

getting udp package info inside unity (GlovePIE) 0 Answers

Destroy(gameObject) fails once in a while 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Problem using 'function Update ()' to restart the game, error CS0246 2 Answers

2D C# destroy a GameObject on collision 2 Answers


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