Reputation: 1606
I am using Unity with c#.
I have two objects and one needs to call a function from the other which is a Singleton. For this I have two solutions. But what's the best thing to do :
- Call for :
MyScript.Instance.MyFunc();
- Store my Singleton object in my calling object and call it like this :
myScript.MyFunc();
What is the best thing to do in term of performance and optimizations.
Thanks a lot.
Upvotes: 0
Views: 265
Reputation:
MyScript.MyFunc();
The class should handle the instance internally, and it should be private. You don't need the "Instance" clutter everywhere, and nobody else needs to know about it.
Upvotes: 0
Reputation: 492
A second variant will be better, because Instance property check current instance for null value every time.
Upvotes: 0