Simple logic question

Hi all, I have a logic question.

When making our static instance, why do we use this format

if (instance != null){
		Destroy(gameObject);
		print ("Music player destroyed");
	} else {
		instance = this;
		GameObject.DontDestroyOnLoad(gameObject)  

Instead of

if (instance = null){
                instance = this;
		GameObject.DontDestroyOnLoad(gameObject)
		
	} else {Destroy(gameObject);
		print ("Music player destroyed");

It looks like it does the same thing to me, but Im sure there is a reason to format it the first way.

There’s no significant reason really, but if we turn it into more of a sentence than code, you’d be saying;

“If we already have an instance of the music player, destroy this gameobject, otherwise we’ll have this one.”

So, your alternative says;

“If we don’t have an instance of the music player, we’ll have this one, otherwise destroy this gameobject”

Not really a lot of difference when you look at it like that… one thing though, other than the first time through, you should always have an instance, so in more cases than not the first option would make sense from a readability perspective, relating to what’s going on.

Also, you’d want;

if (instance == null) {

as you are comparing not setting.

There may be some small performance difference between checking whether something specifically equals something rather than more generally saying “if its not null” but you’d really have to perform a significant test to determine that I think, especially in this case.

Hope this helps.

3 Likes

Thanks, that clears it up quite well for me. I was trying to figure out if I was missing something in the way it is structured, so I’m glad my base assumption that you could write it either way is valid.

Thanks Rob

1 Like

You’re more than welcome. :slight_smile:

Oh, hi! :smiley:

SingletonSpeedTest.zip (2.6 KB)

Just try this to see the difference between using == and != in the if condition, the != is way faster than the == (and if you don’t quit after the first cycles, you can even see the difference taken between the first time, when the singletons are created, and the other ones, when the singletons are already created).

This is the code of the exe:

[code]using System;
using System.Diagnostics;

namespace SingletonSpeedTest
{
class Program
{

    static void Main(string[] args)
    {            
        long avg1 = 0, avg2 = 0;
        UInt64 cycles = 0;
        bool wantToQuit = false;
        while (!wantToQuit)
        {
            Console.Write("Enter number of cycles: ");
            cycles = Convert.ToUInt64(Console.ReadLine());
            avg1 = TestSingleton1(cycles);
            avg2 = TestSingleton2(cycles);
            Console.WriteLine("\nNumber of ticks in Singleton with == in if: " + avg1);
            Console.WriteLine("Number of ticks in Singleton with != in if: " + avg2);
            float perc = (float)(avg2 - avg1) / avg1 * 100;
            Console.WriteLine("Difference in percentage is " + perc+"%");
            Console.Write("\nDo you want to quit? (y/n): ");                
            if (Console.ReadLine() == "y") wantToQuit = true;
        }                  
    }
    
    private static long TestSingleton1(UInt64 cycles)
    {            
        Stopwatch sw = Stopwatch.StartNew();
        for (UInt64 i = 0; i < cycles; i++)
        {
            Singleton1.GetInstance();
        }
        sw.Stop();
        return sw.ElapsedTicks;
    }

    private static long TestSingleton2(UInt64 cycles)
    {
        Stopwatch sw = Stopwatch.StartNew();
        for (UInt64 i = 0; i < cycles; i++)
        {
            Singleton2.GetInstance();
        }
        sw.Stop();
        return sw.ElapsedTicks;
    }
}

class Singleton1
{
    private static Singleton1 _instance = null;
    protected Singleton1() {}
    public static Singleton1 GetInstance()
    {
        if (_instance == null) _instance = new Singleton1();
        return _instance;
    }
}

class Singleton2
{
    private static Singleton2 _instance = null;
    protected Singleton2() {}
    public static Singleton2 GetInstance()
    {
        if (_instance != null) return _instance;
        return _instance = new Singleton2();            
    }
}

}[/code]

1 Like

…and there it is… hehe :smiley: