0

I have three of classes that I am using in my game and I got curious about Singleton pattern.

I have used singleton to access variables or functions easier and I didn't know how to access it without using a singleton pattern only except making "public GameObject" variable and assign object that has a script that I need in inspector.

under here is my sample code

public class ExampleClass : MonoBehaviour
{
    public static ExampleClass instance;
    
    private void Awake()
    {
        instance = this;
    }
}

public GameObject assignScriptObject;

as you see, in the ExampleClass I used singleton pattern that I made and at the las line is how I will assign script in inspector.

Is it fine to use Singleton pattern (I call functions a lot by using that instance) {i.e. ExampleClass.instance.LoadSomeFunctions() } there are dozens of lines with other functions

OR

Is it better to assign script in inspector as an aspect of optimization.

THANKS!

Dev Zoo
  • 1
  • 2
  • You might want to read [What are drawbacks or disadvantages of singleton pattern](https://stackoverflow.com/questions/137975/what-are-drawbacks-or-disadvantages-of-singleton-pattern). I'm not sure why a singleton would have anything to do with performance or optimization. And the issues with mutable global state have been known for a long long time, and there is tons of resources about it. – JonasH Mar 03 '22 at 09:37
  • 1
    To be fair the above code is not sufficient for a singleton. As it doesn’t prevent any other versions just only assigns the instance variable to be the last one made – BugFinder Mar 03 '22 at 09:42
  • The usage of Singleton is quite controversial and often opinion based / specific use case specific. And as @BugFinder already said: Currently there is no Singleton-Pattern in your code. It would become a Singleton by adding e.g. `if(instance && instance != this){ Destroy(gameObject); return; } instance = this;` – derHugo Mar 03 '22 at 10:06
  • The problem with singletons in Unity is generally technical and problems can occur when developers apply singleton patterns that they have used elsewhere in Unity. e.g. the object reference in `private static readonly Lazy _foo` will be nuked by Unity when it recycles the AppDomain whilst the Editor is running. –  Mar 03 '22 at 10:33
  • When I have a need for singletons what I tend to do is to have a GameObject somewhere in the scene, perhaps named "_game"_, which has scripts that I want to be "singletons" attached to it. Then when I want to find the singleton I do something like `public static TracerManager Instance => GetTracerManager(); private static TracerManager GetTracerManager() { if (_instance != null) return instance; var manager = FindObjectOfType(); if (manager != null) { _instance = manager; return manager; }//..not found, lets try GameObject.FindWithTag()...` –  Mar 03 '22 at 10:36
  • 1
    If you're concerned about performance don't be. Either way will process in nanoseconds. Things like shader graphics (4k texture = 16,000,000 pixels to be processed by x amount of functions) are a concern. Simple things like basic data like hitpoints in classes - just don't worry about them, concentrate on finishing the game instead. – Absinthe Mar 03 '22 at 15:27

0 Answers0