Singletons

The Navigator class is associated with the player and represents player controls. There can be one, and only one, player in the scene at any one time. For this reason, the Navigator class should be coded as a Singleton object. A Singleton object is a class that is specifically designed so that it cannot be instantiated more than once. Note that the Singleton design is not a convention by which we agree, with ourselves and other developers, not to instantiate the class more than once. Rather, the Singleton class is designed so as to make multiple instantiations impossible. Let's start implementing Singleton behavior. The class is recoded as follows and the comments follow:

using UnityEngine;using System.Collections;public class ...

Get Mastering Unity 2017 Game Development with C# - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.