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
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.
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: