Game Development Reference
In-Depth Information
Object persistence between scenes is important, but travelling objects take their
baggage with them as they move between scenes. This means any and all child
objects will survive with the persistent object as well as any assets or resources it
uses, such as meshes, textures, sounds, and others. This is not a problem per se, but
it is important to be aware of it. For this reason, many persistent objects are created
light, that is, as empty game objects with no children, featuring only the basic
component makeup they need to work properly. This ensures that only the
essential, critical data survives between scene changes.
Scene Changing
To change the active scene in Unity, use the Application.
LoadLevel function. There are variations on this,
including LoadLevelAsync , LoadLevelAdditive , and
LoadLevelAdditiveAsync . More information on the level-
loading functions can be found online at http://docs.
unity3d.com/ScriptReference/Application.html .
As we saw earlier, the DontDestroyOnLoad function is called on an existing
object in the active scene and prevents that object from being destroyed on future
scene changes. From this, however, an issue sometimes arises concerning object
duplication. Specifically, if you later reload or return to the original scene in which
the persistent object first existed, then a persistent duplicate of the object is made,
namely, the persistent original that came with you from the previous scene and a
newer instantiation of the object created for the newly entered instance of the scene.
This problem is, of course, magnified for each occasion you re-enter the scene, as
each time a new duplicate will be made. Such duplication is usually not what you
want. You typically want only one instance of the object to exist at any one time: one
player, one game manager, or one high-score board. To achieve this, you'll need to
create a singleton object, as explained in the next section.
 
Search WWH ::




Custom Search