I am wondering which is the right way to use singleton instance: when I create a singleton Class called "Manager" and it contains an int variable called "value" and I have a another class called "A"...
//singleton class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Manager : MonoBehaviour {
public static Manager Instance{ set; get;}
public int value;
private void Awake () {
if (Instance == null) {
Instance = this;
DontDestroyOnLoad (this.gameObject);
} else {
Destroy (gameObject);
}
}
}
So inside my A class I can make a instance of the singleton like this: // example pseudo code public class A {
// First
// using as a global variable
Manager manager;
//------------------------------------
Public void tstOne(){
// First
// using that global variable
manager.Instance.value = 0;
// Second
// or this way
Manager.Instance.value = 0;
}
Public void tstTwo(){
// First
// using that global variabl
manager.Instance.value = 1;
// Second
// or this way
Manager.Instance.value = 1;
}
}
So my problem is - should I create A global instance and then use that instance like in first one or should I use the second example?
Are they both are same in terms of memory consumption and efficiency? Or is there another way to use singletons?