Skip to content

Commit ecf7c42

Browse files
Erik Bylundkirre-bylund
authored andcommitted
fix: Make LootLockerServerAPI use FindObjectsByType instead of tag
1 parent 53fe4a3 commit ecf7c42

File tree

1 file changed

+8
-23
lines changed

1 file changed

+8
-23
lines changed

Runtime/Client/LootLockerServerApi.cs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using LootLocker.LootLockerEnums;
99
using UnityEditor;
1010
using LootLocker.Requests;
11+
using Object = UnityEngine.Object;
1112
#if UNITY_EDITOR
1213
using UnityEditorInternal;
1314
#endif
@@ -26,6 +27,7 @@ public class LootLockerServerApi : MonoBehaviour
2627
private static int _instanceId = 0;
2728
private const int MaxRetries = 3;
2829
private int _tries;
30+
public GameObject HostingGameObject = null;
2931

3032
public static void Instantiate()
3133
{
@@ -37,8 +39,9 @@ public static void Instantiate()
3739
gameObject.tag = "LootLockerServerApiGameObject";
3840
}
3941

40-
_instanceId = gameObject.GetInstanceID();
4142
_instance = gameObject.AddComponent<LootLockerServerApi>();
43+
_instanceId = _instance.GetInstanceID();
44+
_instance.HostingGameObject = gameObject;
4245
_instance.StartCoroutine(CleanUpOldInstances());
4346
if (Application.isPlaying)
4447
DontDestroyOnLoad(_instance.gameObject);
@@ -47,17 +50,13 @@ public static void Instantiate()
4750

4851
public static IEnumerator CleanUpOldInstances()
4952
{
50-
if (!_bTaggedGameObjects)
53+
LootLockerServerApi[] serverApis = GameObject.FindObjectsByType<LootLockerServerApi>(FindObjectsInactive.Include, FindObjectsSortMode.None);
54+
foreach (LootLockerServerApi serverApi in serverApis)
5155
{
52-
yield break;
53-
}
54-
GameObject[] gameObjects = GameObject.FindGameObjectsWithTag("LootLockerServerApiGameObject");
55-
foreach (GameObject gameObject in gameObjects)
56-
{
57-
if (_instanceId != gameObject.GetInstanceID())
56+
if (serverApi != null && _instanceId != serverApi.GetInstanceID() && serverApi.HostingGameObject != null)
5857
{
5958
#if UNITY_EDITOR
60-
DestroyImmediate(gameObject);
59+
DestroyImmediate(serverApi.HostingGameObject);
6160
#else
6261
Destroy(gameObject);
6362
#endif
@@ -84,20 +83,6 @@ private static void OnEnterPlaymodeInEditor(EnterPlayModeOptions options)
8483
{
8584
ResetInstance();
8685
}
87-
88-
[InitializeOnLoadMethod]
89-
private static void CreateTag()
90-
{
91-
if (InternalEditorUtility.tags.Contains("LootLockerServerApiGameObject"))
92-
{
93-
_bTaggedGameObjects = true;
94-
}
95-
else
96-
{
97-
InternalEditorUtility.AddTag("LootLockerServerApiGameObject");
98-
_bTaggedGameObjects = true;
99-
}
100-
}
10186
#endif
10287

10388
public static void SendRequest(LootLockerServerRequest request, Action<LootLockerResponse> OnServerResponse = null)

0 commit comments

Comments
 (0)