Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

I have the tendency to overuse singletons...

Discussion in 'General Discussion' started by Luiz_Thiago, Aug 10, 2017.

  1. Braineeee

    Braineeee

    Joined:
    Nov 9, 2014
    Posts:
    1,211
    So I've been a chronic singleton for a while now, and its a problem. Help? :p
     
  2. mysticfall

    mysticfall

    Joined:
    Aug 9, 2016
    Posts:
    649
    On a side note, it just occurred to me that people might have a wrong idea from my own game project which is linked in my signature.

    I doubt if anyone would have actually studied the source code, but they might have read the description and think that I'm on some sort of a crusade to urge people to abandon the way things are done in Unity, and adopt some 'leet' method like DI instead.

    It's not really. You can see that project as a personal experiment of mine, where I can try whatever extreme approach I feel like trying. I have zero intention to urge others to follow my mehod, at least not at this stage, because even I don't know if it'd turn out to be a viable approach at all.

    Using a DI container like Zenject doesn't really force you to abandon MonoBehaviours and writing everything as POCO, or make you lose your ability to set up and configure components in the inspector panel.

    All it changes is that it lets you wire those components up in your code and easily change them, instead of dragging and dropping components to each other's properties all the time.
     
    Last edited: Aug 19, 2017
  3. mysticfall

    mysticfall

    Joined:
    Aug 9, 2016
    Posts:
    649
    Don't worry. Go grab a chalk and write these words 100 times and you are cured :D :

     
    frosted likes this.
  4. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Firstly - it's OK to use whatever you want. I'm not telling you that DI in Unity is bad.

    I prefer my Unity components to be as explicit as possible because again, I work with those components from the editor quite often. Perhaps most frequently when debugging something, as working with the specific environment as instanced is important.

    Because of the way I work there, I prefer my definitions be very narrow and my dependencies to be crystal clear. In my experiences, I've found that pumping variables in through implicit context tends to be cumbersome to work with in the long run (in Unity specifically).

    I'm not saying that you shouldn't use DI, I'm not even saying that other's shouldn't try it (you totally should), I'm saying that in my experience, there are better approaches to working within Unity.
     
    Last edited: Aug 19, 2017
    passerbycmc and mysticfall like this.
  5. gargaroots

    gargaroots

    Joined:
    Dec 8, 2016
    Posts:
    4
    LMFAO!!!!! Thanks for the laugh, you made my day lighter. :)