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 Rush3fan · Jul 18, 2012 at 07:00 PM · loadmemorylevelresetexit

Exiting from a scene properly

Hi. I'm a little confused on what I'm doing wrong. I want to exit from a race and back to the menu, but when I enter a race for the second time, the console fills with thousands of errors.

When I exit from a race, I reset all static variables manually before loading back to the main menu. The main menu works just fine, but it seems like there is something stuck in the memory that's causing the game to crash like this.

Anyone have any ideas what other things I need to do to get a full memory reset when I load new levels?

Comment
Add comment · Show 21
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 Mortoc · Jul 18, 2012 at 07:32 PM 0
Share

All your gameObjects are reset when you enter a new scene (except anything that's marked as DontDestroyOnLoad). It sounds like your problem is in your static variables, they are not reset between levels. Additionally a build of the game will handle statics differently than the editor, so there's a good chance for bugs there as well.

If you're going to use loadlevel as your encapsulation method for levels, my recommendation is to use as few statics as possible (most cases: zero), since they are not encapsulated by loadlevel.

avatar image Bovine · Jul 18, 2012 at 07:35 PM 1
Share

We're going to need some more context before we can help. What do you mean exit a race - are you playing a racing game then? Is this all your own work? A sample?

If you're reseting a bunch of static variables, that could be exactly the right thing to do or entirely the wrong thing. For instance, in our Dungeon Crawler, we have a lot of statics for singleton manager classes that will persist for the entire game, if we wiped those out, the game would blow up in a similarly stunning way perhaps.

Are you sure all these statics need reseting? By reseting do you mean setting to null or do you mean calling some Reset() on?

avatar image Bovine · Jul 18, 2012 at 07:44 PM 1
Share

Never is a bit strong, statics are best used for singleton instances I$$anonymous$$HO and this is a very good and, from my experience, very common pattern for Unity. Using them for other things, is of course, madness :)

avatar image Bovine · Jul 18, 2012 at 08:06 PM 1
Share

If you're using LoadLevel and you don't have anything marked as DontDestoryOnLoad then your game will destroy everything level to level except what you've stored in statics.

@$$anonymous$$ortoc I'm taking about where you have a manager for some large aspect of the game that you need to get at everywhere. We typically instantiate this at startup and provide a getter property as a static. We might have a dozen such statics in the form, so we can do something like.

DWItem$$anonymous$$anager.Instance.DropItem(item, transform);

Before we were doing this, it would otherwise be a case of finding the object the singleton was attached to. Such patterns are common not just in Unity of course. So I guess I disagree strongly - it helps a lot and I can't see how it is dangerous and difficult to maintain.

if on the other hand you have;

Player.CurrentHealth = 100; Player.Lives = 3;

Then that certainly becomes unmanageable.

avatar image Bovine · Jul 18, 2012 at 08:29 PM 1
Share

The instance is attached to a GameObject, so that it can have references to other objects and scripts in the scene dragged to it for convenience. Otherwise it could be a static method. I probably would still use the singleton pattern though I guess because I grew up with C++ and that trusty singleton pattern :o)

Show more comments

1 Reply

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

Answer by Kryptos · Jul 18, 2012 at 07:38 PM

Might be related to references to other objects that are still being held by a persistent object (an object with DontDestroyOnLoad). Because the pointed objects don't extist anymore but you're trying to access them when loading again the level, it casts some NullReferenceException.

The following methods may help you free those references before or after a level load:

  1. MonoBehaviour.OnLevelWasLoaded

  2. MonoBehaviour.OnDestroy

  3. MonoBehaviour.OnDisable

Then you might need to find new references for your variable, using one of those methods:

  1. Object.FindObjectOfType

  2. GameObject.Find


edit: Your issue could be related with this one in fact: Avoid NullReferenceException with Singleton


edit2: Even if you don't use singleton. The methods above can also be used to reset static variables to consistent values.

Comment
Add comment · Show 4 · 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 Rush3fan · Jul 18, 2012 at 07:57 PM 0
Share

I never used DontDestroyOnLoad. I have tried to set this up as simple as possible.

avatar image Bovine · Jul 18, 2012 at 08:10 PM 1
Share

The pattern we use here is:

  • Scene 0 contains everything that will persist throughout the game and marked as DontDestroyOnLoad

  • We have a DWGameFramework class that is instantiated in Scene 0 along with a number of other manager classes that provide convenient access to things like the item system, UI, etc..

  • Subsequent scenes are loaded using LoadLevel()

I believe this pattern and variations thereof is also quite common.

avatar image Rush3fan · Aug 02, 2012 at 09:02 AM 0
Share

The whole problem came down to all the static variables I had scattered throughout the project. Another problem was just that - they were scattered and the solution was putting them all in one dedicated script that was placed in an object that persisted through all stages in the game. That way, by loading a new scene, you aren't deleting the script that has the static vars that needed to be reset in a different scene. So, lesson learned: keep your static vars well organized to avoid chaos in your error console. It's that simple.

avatar image Rush3fan · Aug 02, 2012 at 09:08 AM 0
Share

Oh, and for all reading this who have already made the same mistake: there is a search feature in $$anonymous$$onoDev(script editor) that looks through every script in your entire project. That in it's self saved me hours of repair time. You just enter "static" and it finds everything.

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

7 People are following this question.

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

Related Questions

Multiple calls of Resources.Load for the same resource 1 Answer

Post-Build Custom Level Loading 2 Answers

load level on collision 2 Answers

How do I get the MainCamera to trigger application.loadLevel when it hits a collider? 2 Answers

How the scene works 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