Alternative to singletons
Hello everyone,
My program uses a MonoBehaviour called ActionBuffer. This class interact with others MonoBehaviour called Actor and should be accessible for every Actor when they would send actions to buffer.
The point is how to get ActionBuffer from an Actor class. Of course, ActionBuffer should be instantiated only once, and for now I used it as a singleton. I'm in an early stage of creation and want to begin with the best coding practices I can. I know some says prematural optimization is evil but so are singletons apparently...
I already searched about alternatives and found some threads about dependency injection or toolbox. But I can't understand how dependency injection is used with MonoBehaviour since you should not use constructors with them or maybe I don't get the point at all? And toolbox just seems to be an agglomeration of singletons in order to have only one remaining, so not really what I look for.
Would be great to know if you have any suggestions, topics, tutorials or everything else that could be related to best practices about different MonoBehaviour interactions.
There are lots of older threads here about Unity Singletons. $$anonymous$$ight also be helpful to search something like "Design Patterns Overused."
Check out the DI frameworks made specifically for Unity like Zenject / StrangeIoC. They solve the problem with $$anonymous$$onoBehaviour constructors that you mention
Your answer
Follow this Question
Related Questions
Referencing monobehaviour singleton from a non-monobehaviour class? 1 Answer
NullReferenceException: Object reference not set to an instance of an object in Singleton class. 1 Answer
What is going on with null reference checks recently? 0 Answers
MonoBehaviour script duplicates when loading same scene multiple times 1 Answer