What is difference between Singleton Pattern and Static keyword?

vs

1 Like

Hi Kakushadze,

The Singleton Pattern enables you to ensure that only one instance of an object exists in, well, lets say your game. If another object at that same type is created, your Singleton pattern would destroy the second instance, ensuring only the first remains.

When you use the static modifier on a member variable you are stating that the member variable belongs to the type and not an instance.

As an example, if you had something like this;

using UnityEngine;

public class Block : MonoBehaviour
{
    private static int count;
}

You could update the value of count in any of your Blocks and they would all share the same value, because the member variable now belongs to the type instead of an instance. If you didn’t use the static keyword, each block could update its own count.

Hope this helps :slight_smile:


See also;

1 Like

Privacy & Terms