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. Dismiss Notice

Question storing singleton reference or not for performance benefit?

Discussion in 'Scripting' started by marchall_box, Dec 30, 2020.

  1. marchall_box

    marchall_box

    Joined:
    Mar 28, 2013
    Posts:
    139
    Hi,

    Would I get much more benefit by storing frequently used references within the instance ?

    Code (CSharp):
    1. void Update()
    2. {
    3.     GameManager.Instance.playerCombat.DoSomething();
    4. }
    vs

    Code (CSharp):
    1. void Start()
    2. {
    3.     PlayerCombat _pc = GameManager.Instance.playerCombat;
    4. }
    5. void Update()
    6. {
    7.     _pc .DoSomething();
    8. }
     
  2. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    You will get a benefit as soon as accessing the singleton every frame becomes a bottleneck. As in, once you have performance issues and you profiled your game and you figured the problem is accessing the Instance property every frame, then, and only then, you should worry about it.
     
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Nope.
     
    PraetorBlue likes this.
  4. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    Very informative :D