Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Runtime/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ namespace FishingCactus.CommonCode
public class Singleton<_INSTANCE_> : MonoBehaviour
where _INSTANCE_ : Singleton<_INSTANCE_>
{
// -- FIELDS

[SerializeField] private bool DestroyOnLoad = false;

private static _INSTANCE_ _instance = null;

// -- PROPERTIES
[SerializeField] private bool DestroyOnLoad = false;

public static _INSTANCE_ Instance
{
Expand All @@ -28,7 +24,12 @@ public static _INSTANCE_ Instance

public static bool HasInstance => _instance != null;

// -- UNITY
public static bool TryGetInstance( out _INSTANCE_ instance )
{
instance = Instance;

return instance != null;
}

public virtual void Awake()
{
Expand All @@ -43,10 +44,10 @@ public virtual void Awake()
}
else if( _instance != this )
{
Debug.LogError( $"{_instance.name} is added two times.", this );
Debug.LogWarning( $"There are two instances of {_instance.name}. The latest one will be destroyed.", _instance );

Destroy( this );
}
}
}
}
}