Untangling GameObject State in Unity

In this short post and video I try to discuss and clarify a few points about GameObject state in Unity with respect to game, scene and “runtime” scope. It is a bit more complex than one may understand initially, so bear with me a little.

When you start developing scenes in Unity, it won’t take long before you start asking questions like:

How can I get the same GameObject in different scenes?

Why do static properties sometimes get reset across scenes?

Why when I reload a scene I get duplicated objects which are meant to be singletons?

How can I comply with the (highly practical) principle “Make the game runnable from every scene” when I have global instances from other scenes?

Here are some answers.

Here is the full schema I refer to:

GameObject State in Unity

While MonoBehaviour’s  life-cycle is quite well documented e.g. both directly in Unity docs here and also by third parties e.g. here, how to handle GameObject’s persistence in and across scenes may be more obscure.

So here are written (partial) answers to the questions above:

How can I get the same GameObject in different scenes?
Would be probably better to reword this as “how can I persist and share data across scenes” – and there are many ways to do that :-)

Why do static properties sometimes get reset across scenes?
Only static properties which are GameObjects present in the scene will get reset (typically singletons). Other static properties will be preserved across the virtual machine.

Why when I reload a scene I get duplicated objects which are meant to be singletons?
That is because you marked those objects with DontDestroyOnLoad and created them (also) in other scenes. Create them via code (not in hierarchy) checking before their existence.

How can I comply with the (highly practical) principle “Make the game runnable from every scene” when I have global instances from other scenes?
This is best done just as explained above: create the global objects in every scene via code if they don’t already exist.

 

Unity Execution Order

Unity Execution Order – from Unity documentation.

If you are in need of learning some good patterns in software game development, a really nice book is Game Programming Patterns. Here are also 50 Tips for Working with Unity (Best Practices) which I reread from time to time, understanding progressively more and more of them (but still not all :D).

Thanks to Daniele Giardini for some feedback on the state scheme above.

Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, applied / serious games.

Social Share Toolbar

Comments

  1. Maybe I missed it but at which point in the video do you refer to “Why do static properties sometimes get reset across scenes?”?

    • Pietro Polsinelli says:

      I think I do mumble it sometime, but anyway its this: if your static property is a game object (typically a singleton), it will be reset at scene unload, differently from all other static properties.

      Actually maybe should write written answers to all questions from top.

      • So this basically happens when my script (that contains the static properties) is extending from MonoBehaviour and is not marked as DontDestroyOnLoad, right?

        And yeah, having the answers written down would nice :)

        • Pietro Polsinelli says:

          Careful: this answer (I added written answers)

          Why do static properties sometimes get reset across scenes?
          Only static properties which are GameObjects present in the scene will get reset (typically singletons). Other static properties will be preserved across the virtual machine.

          does not imply that all static properties will be reset. Any static property on the component classes that is not a reference to a GameObject will not get reset, even if the “carrying” GameObject is.

          • I think I now understand. If I have something like: “public static GameObject LastKilledEnemy;” then it can get reset on a Scene reload because the reference to that GameObject has vanished.

            Also thanks for the written answers!

Speak Your Mind

*