- Home /
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?
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.
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?
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 :)
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.
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)
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:
Then you might need to find new references for your variable, using one of those methods:
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.
I never used DontDestroyOnLoad. I have tried to set this up as simple as possible.
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.
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.
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.