Skip to content

Commit aab66ed

Browse files
authored
Merge pull request Habrador#3 from masoudarvishian/master
Improve the implementation of SingletonUnity.cs
2 parents 906850c + e92332e commit aab66ed

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

Assets/Patterns/5. Singleton/Scripts/SingletonUnity.cs

+28-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
31
using UnityEngine;
42

53
//
@@ -28,43 +26,46 @@ public static SingletonUnity Instance
2826
{
2927
if (instance == null)
3028
{
31-
//If a script in Unity inherits from MonoBehaviour, we can't use the new keyword to create a new Singleton as we did before
32-
//So you have to manually add this script to a gameobject in the scene
33-
//But because we inherit from MonoBehaviour whem might have accidentally added several of them to the scene, which will cause trouble, so we have to make sure we have just one!
29+
// Find singleton of this type in the scene
30+
var instance = GameObject.FindObjectOfType<SingletonUnity>();
3431

35-
//Find all singletons of this type in the scene
36-
SingletonUnity[] allSingletonsInScene = GameObject.FindObjectsOfType<SingletonUnity>();
37-
38-
if (allSingletonsInScene != null && allSingletonsInScene.Length > 0)
32+
// If there is no singleton object in the scene, we have to add one
33+
if (instance == null)
3934
{
40-
//Destroy all but one singleton
41-
if (allSingletonsInScene.Length > 1)
42-
{
43-
Debug.LogWarning($"You have more than one SingletonUnity in the scene!");
44-
45-
for (int i = 1; i < allSingletonsInScene.Length; i++)
46-
{
47-
Destroy(allSingletonsInScene[i].gameObject);
48-
}
49-
}
50-
51-
//Now we should have just one singleton in the scene, so pick it
52-
instance = allSingletonsInScene[0];
35+
GameObject obj = new GameObject("Unity Singleton");
36+
instance = obj.AddComponent<SingletonUnity>();
5337

5438
//Init the singleton
5539
instance.FakeConstructor();
56-
}
57-
//We have no singletons in the scene
58-
else
59-
{
60-
Debug.LogError($"You need to add the script SingletonUnity to a gameobject in the scene!");
40+
41+
// The singleton object shouldn't be destroyed when we switch between scenes
42+
DontDestroyOnLoad(obj);
6143
}
6244
}
6345

6446
return instance;
6547
}
6648
}
6749

50+
void Awake()
51+
{
52+
if (instance == null)
53+
{
54+
instance = this;
55+
56+
// Init the singleton
57+
instance.FakeConstructor();
58+
59+
// The singleton object shouldn't be destroyed when we switch between scenes
60+
DontDestroyOnLoad(this.gameObject);
61+
}
62+
// because we inherit from MonoBehaviour whem might have accidentally added several of them to the scene,
63+
// which will cause trouble, so we have to make sure we have just one!
64+
else
65+
{
66+
Destroy(gameObject);
67+
}
68+
}
6869

6970

7071
//Because this script inherits from MonoBehaviour, we cant use a constructor, so we have to invent our own

0 commit comments

Comments
 (0)