Understanding scenes in Unity3d

user2853108 picture user2853108 · Jan 4, 2014 · Viewed 18.8k times · Source

I have some confusion with scenes in Unity3d and I was not able to find any resources about them.

  1. When should scenes be used? For example in a platformer would every level have to be a different scene? Would the main menu be a scene?
  2. Can one overlay scenes?
  3. How do assets work between scenes? Are they attached to each individual scene and have to be reloaded every time. Can one specify when an asset is no longer needed?
  4. How does one send data between scenes/interface between scenes? I understand that this is a broad topic, but I didn't want to spam with multiple questions.

Answer

Heisenbug picture Heisenbug · Jan 4, 2014

When should scenes be used? For example in a platformer would every level have to be a different scene? Would the main menu be a scene?

There are no general rules about that. In theory you may have just one scene for the whole game. How you organize your scenes is entirely up to you and often depends on the type of game you are creating.

I think that there are at least 3 features to be considered of using scenes:

  • they are a logical container for all pre-instantiated objects that might be useful to divide your game into multiple levels/sections.
  • You can serialize cross references between GameObjects and Components inside a scene (if GO A needs a ref to GO B, and they belong to the same scene, the reference can be serialized and you no longer need to find the referenced object at runtime)
  • When you load (not in an additive way) another scene, the resources already loaded into memory are automatically released

Can one overlay scenes?

Yes you can using LoadAdditive. Unfortunately, once 2 scenes are both loaded into memory there is no automatic way of distinguish objects belonging to one or the other. So if you load additive a second level environment, it's up to you to keep track of the previous environment and explicitly destroy it if you need to.

How do assets work between scenes? Are they attached to each individual scene and have to be reloaded every time. Can one specify when an asset is no longer needed?

As defaults every GameObject of a scene will be destroyed once the new scene is loaded (unless you use an additive scene loading). A way to make a GameObject survive across scenes is to mark it using DontDestroyOnLoad.

If you need to share a particular "configuration" of a GameObject, you can store it as a prefab, and reference it across scenes (but remember that once in a scene it's a prefab instance, so the GO shares with the prefab the initial serialized and not overriden properties, but 2 instances of the same prefab are different objects).

How does one send data between scenes/interface between scenes?

Several ways, depending on what kind of persistent data you want to share.

  • For a particular GameObject instance let the object survive using DontDestroyOnLoad.
  • If you have some configuration data that doesn't need to be attached to a specific GameObject you can consider storing a ScriptableObject inside the AssetDatabase and reference it.
  • If you have data that must persist across different game sessions you can consider storing them into PlayerPrefs.

There are 2 other ways that I don't like, but just to cite them:

  • Using a static field can sometimes help you in doing that, but it has several problems from my point of view
  • Save and load from disk (could be useful in several situations, but often it's a platform dependent way and you can have some trouble especially on different mobile platforms)

This is a broad topic btw, I hope this answer can be a quite decent overview.