Search Unity

Singleton has a performance impact

Discussion in 'Scripting' started by MikeyJY, Apr 15, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I recently hear about singletons and I know that unity is assuming that you are use only one gameObject in the scene with a component. Instead of using GameManager.GetComponent<Inventory>().method() is just Inventory.method(). Some of my scripts don't use singletons because I didn't know about them. This things are just for an easier access or they are improving performance? I'm asking because if they are only to facilitate the access of some references I don't need to update the scripts that aren't using them, but if they have any performance
    improvement have have to use them anywhere.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    They aren't for performance improvement.

    Singletons serve 2 general purposes.

    Enforcing that only 1 instance of the object exists. Creating an access point for that single instance (usually global).

    If you needs these aspects, than use it. If you already have a working design that doesn't employ singletons, you don't have to use it. Heck, there are arguments out there for why singletons are bad (it often uses a global for the access point, and globals are often considered bad).

    Basically though... if you don't know why you're using a design pattern, than you probably shouldn't be using it.
     
    Kurt-Dekker and Joe-Censored like this.
  3. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,451
    What @lordofduct said but if you do the above in Update or similar often called methods, do yourself a favor and fetch that component in Start or Awake, store the reference on a member variable of your class and use that reference to call your method from.
    GetComponent WILL have a performance impact, but it isn't significant enough to use Singletons for just this reason and doing it only once in those methods is all the optimization you'd want to do there
     
  4. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Thank you for informations
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    You could also just have your GameManager (which sounds like it is a singleton in and of itself) could cache those references so you don't have to GetComponent on it all the time.
     
    MartinTilo likes this.
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The use of "singleton" in regards to Unity is often done more loosely than what you will read about the design pattern. Often singleton is just used to refer to a single instance of something you will access from many places, and not strictly follow the "rules" for a true singleton in an academic sense.

    For example, often times people will call some manager script in their game a singleton and leave out lazy initialization, since they are attaching it to a GameObject in the scene itself, so there should never be an instance where lazy initialization would take place. Is it really a singleton then? I personally think that question itself is rather meaningless, nor do I think it is a problem. Don't get so hung up on the specifics of what a singleton is supposed to be IMO.

    Game development often is about bending or breaking regular programming rules championed by those who wrote the rules to help them create enterprise level applications, but those rules are often a bad fit for game development. Just make something that works, doesn't cause an oversized amount of headache to maintain, and conforms to your requirements for what you wanted to create.