Book Image

Mastering Unity Scripting

By : Alan Thorn
Book Image

Mastering Unity Scripting

By: Alan Thorn

Overview of this book

Table of Contents (17 chapters)
Mastering Unity Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Immortal objects


By default, Unity regards every object as existing within the self-enclosed time and space of a scene. The difference between scenes is like the difference between separate universes. Consequently, objects don't survive outside the scene to which they belong; this means that they die whenever the active scene changes. This is typically how you want objects to behave, because scenes are usually very different and separate from one another. However, even so, there will be objects that you don't want destroyed. There will be objects that you need carried over between scenes, such as the Player character, a high-score system, or a GameManager class. These are normally high-order objects whose existence shouldn't be limited to specific scenes; they should rather span or arc across multiple scenes. You can create object persistence easily using the DontDestroyOnLoad function, but it has important consequences worth considering. Take a look at the following code sample 3-12:

using...