Search Unity

Unity Tips - The Singleton Pattern

Discussion in 'Community Learning & Teaching' started by mathiassoeholm, Jul 23, 2012.

  1. mathiassoeholm

    mathiassoeholm

    Joined:
    Jul 20, 2010
    Posts:
    104
    Hi everyone

    We've continued our Unity series, and this time I wrote a bit on using the singleton pattern with Unity.
    http://clearcutgames.net/home/?p=437

    Feedback is always appreciated :)
     
  2. Nomad72

    Nomad72

    Joined:
    Oct 25, 2011
    Posts:
    117
    Thank you, this was a nice article! A very simple, and very useful, coding tip.
     
  3. OceanBlue

    OceanBlue

    Joined:
    May 2, 2013
    Posts:
    251
    Thanks ClearCut... I was never sure about Singletons (just starting to learn C# after a few years with UnityScipt)... but your blog has made a lot more sense of it to me now. Bookmarked!
     
  4. IndieForger

    IndieForger

    Joined:
    Dec 31, 2012
    Posts:
    92
    Thanks! I wrote Creating Game Manager using State Machine and Singleton pattern in Unity3d which uses singleton to persist manager, preventing its destruction when different scene is loaded.
    I have used your singleton approach to store just one instance of an object.
     
  5. IndieForger

    IndieForger

    Joined:
    Dec 31, 2012
    Posts:
    92
    Here is the code I used in the post:

    public class Singletone {
    protected Singletone() {}
    private static Singletone _instance = null;

    public static Singletone Instance { get {
    return Singletone._instance == null ?
    new Singletone() : Singletone._instance;
    } }
    }
     
  6. abhimanyuOnline07

    abhimanyuOnline07

    Joined:
    Dec 17, 2013
    Posts:
    1
    Code (csharp):
    1. public class GameManager
    2. {
    3.  
    4.     private static GameManager instance = null;
    5.    
    6.  
    7.     public static GameManager GetInstance()
    8.     {
    9.         if(instance == null)
    10.         {
    11.             instance = new GameManager();
    12.         }
    13.        
    14.         return instance;
    15.     }
    16. }
     
  7. LouisSong

    LouisSong

    Joined:
    Apr 13, 2013
    Posts:
    4
    The MathFul game sounds interesting,where can we download it,and can you share the source code with us?:)
     
  8. daviddickball

    daviddickball

    Joined:
    May 26, 2014
    Posts:
    10
    Thanks, I keep reading this method mentioned but could never find a guide!
     
  9. Mikozouzou

    Mikozouzou

    Joined:
    Nov 25, 2014
    Posts:
    1
    Thanks for this article, it will probably help me for my current project. :)

    As a feedback, I would say that your last two images are corrupted or something else, so they're not displayed anymore !
     
  10. li3ro

    li3ro

    Joined:
    Jul 11, 2015
    Posts:
    2

    You must use some sort of locking between your if(instance == null) and your instatiation since it might happen that you passed the nullity check and another thread will create new instace at the same point cause he passed the nullity check as well - than your thread and the other thread continues and both create new instance - 2 instances is not a singleton

    The right way to go in your path is :
    Check nullity > lock the thread > check nullity again > create new instance > release lock and return.
     
  11. 4nc3str4l

    4nc3str4l

    Joined:
    Dec 1, 2014
    Posts:
    1
    Thanks a lot! With this I have solved a tone of bugs...

    This was really helpfull :)
     
  12. Quinnje012

    Quinnje012

    Joined:
    Nov 30, 2015
    Posts:
    7
    I know this is an ooold post, but the last two images on the blog are broken (and I didnt want to use the facebook posting system on the blog).