Which of these ways of function calling from a Singleton script is preferred?

Hi,

I use three ways to call functions from other manager scripts (which are all Singletons, and are all components of the Manager gameobject), and I’m not sure which one is preferred, and why:

Way #1 - Creating a SomeManager variable that I re-use throughout the whole script, by grabbing it from the Managers gameobject.

private SomeManager someManager;

void Awake(){
someManager = GameObject.Find("Managers").GetComponent<SomeManager>();
// Or assign it in inspector
}

void function1(){
someManager.someFunction();
}

void function2(){
someManager.someOtherFunction();
}

or Way #2 - Always using the .Instance function to call

void function1(){
someManager.Instance.someFunction();
}

void function2(){
someManager.Instance.someOtherFunction();
}

Or Way #3 - Using the instance function to create a variable that allows me to call the functions:

private SomeManager someManager;

void Awake(){
someManager = SomeManager.Instance;
}

void function1(){
someManager.someFunction();
}

void function2(){
someManager.someOtherFunction();
}

Any suggestions? Thanks.

The second one is the only one that follows a singleton design.

The singleton script should have a static variable that targets itself. Usually you use a property to access it so you can make sure it has a value, if it does, you use that value, if it doesn’t, you will assign it itself as the value which is then used when you access it.

With this in mind, your other scripts that access it will always access through the instance variable and not need to create a reference to the script of it’s own. While your third method could save you on typing Instance, I would still use the second version.

Your first method defeats the purpose of a singleton design completely.

2 Likes

Second way is the best way. After you start using it, you’ll see the reasons.

1 Like

You don’t really need access to the singleton Instance externally. If you need a singleton, it reads better if you wrap it’s methods in static methods:

public class SomeManager : MonoBehaviour {

    private static SomeManager instance;

    void Awake() {
        instance = this;
    }

    public static void Function1() {
        //use instance here
    }
}

//Now your calling code is:
SomeManager.Function1();

Since it’s the same class, you have full access to the instance’s privatte variables and methods. If it becomes clunky to prefix everything with instance, you can just call a member method on the instance:

public static void Function1() {
    instance.function1();
}

privare void function1() {
    //do stuff
}

Note that unless SomeManager needs to call Update or Awake or whatever, you might as well have it be a static class instead of a singleton object.