I'm making a unity game, and in my game I have several scripts attached to persistent game objects, when I need to get a value from a different object (such as getting the health from the player object and using it in the UI) I would use ExampleClass exampleClass; exampleClass = FindObjectOfType<ExampleClass>
And because there is only ever one of the gameobject this worked fine but I learned about singletons and realized it would make more sense to change to using singletons but now I have run into two different methods of using these singletons.
The first is to create a new object in each class that needs to reference the singleton, like I was doing before but to get the instance from the singleton class and store it in this new object and then reference that object. ExampleClass exampleClass; exampleClass = ExampleClass.Instance
But I have also seen the class directly referenced instead of making a new object. int x; x = ExampleClass.Instance.y
This seems to make more sense to me intuitively but I'm not sure if there is a performance reason to make new objects in every class or if its just for conveniences sake.
TLDR: When using singletons in unity is it better to do int x; ExampleClass exampleClass; exampleClass = ExampleClass.Instance; x = exampleClass.y;
or should I do int x; x = ExampleClass.Instance.y