Game Development Reference
In-Depth Information
void Start ()
{
//Make game manager persistent
DontDestroyOnLoad(gameObject);
}
//-----------------------------------------
}
//-----------------------------------------
This object will persist across scenes, but how can it (or any class like it) become a
singleton object? The following code sample 3-14 demonstrates how:
01 using UnityEngine;
02 using System.Collections;
03 //-----------------------------------------
04 //Sample Game Manager class - Singleton Object
05 public class GameManager : MonoBehaviour
06 {
07 //-----------------------------------------
08 //C# Property to get access to singleton instance
09 //Read only - only has get accessor
10 public static GameManager Instance
11 {
12 //return reference to private instance
13 get
14 {
15 return instance;
16 }
17 }
18
19 //-----------------------------------------
20 private static GameManager instance = null;
21 //-----------------------------------------
22 //High score
23 public int HighScore = 0;
24
25 //Is game paused
26 public bool IsPaused = false;
 
Search WWH ::




Custom Search