By definition, a singleton, at least in terms of object oriented programing, is a class that can have only one instance. That one instance is stored and referenced statically.
Here is the code necessary to create a simple singleton.
public class GameData
private static GameData s_Instance;
public static GameData instance
if (GameData.s_Instance == null)
GameData.s_Instance = new GameData();
// Your code here...
public int hitPoints;
The code above creates a simple singleton class. Of course, if you are using this in Unity, you may want to use some of the standard Unity functions, such as FindObjectOfType. In this case, you will need to inherit from one of the standard Unity classes, such as ScriptableObject. These classes don’t like to be instantiated with new. Instead, they rely on function calls to create instances, so the code needs to be modified to accommodate that.
public class Localization : ScriptableObject
private static Localization s_Instance;
public static Localization instance
if (Localization.s_Instance == null)
Localization.s_Instance = ScriptableObject.CreateInstance<Localization>();
// Your code here...
public string language;
The region/endregion statements are not necessary. They are there simply to allow the whole region to be collapsed in the IDE. Add whatever other code you want to be part of the class, and you are ready to go.
In this example, you can refer to the string language from anywhere in your code by accessing the static instance of the Localization class as in the following example:
Localization.instance.language = "en-us";
This code can be called from anywhere in your program, without the need to instantiate an object. The first time access Localization.instance, it is instantiated.
Here’s a quick tip on how to Ping from Python!
Today, I had a need to do a quick ping test from a Python script. It took me a bit of searching to figure out how to do it properly. Most of the examples I saw in the wild ended up displaying results to the screen (or stdout), which is not at all what I was looking for.
Here is what I ended up with:
import sys # This import is only needed to get command line arguments.
def ping_test(host, ping_count=1):
# Some systems use different parameters for ping count
# Linux and MacOS use -n, Windows uses -c
# Adjust this as necessary for other systems
count_param = '-n' if platform.system().lower() == 'windows' else '-c'
# subprocess.Popen takes a list of parameters, starting with the command to run
command = ['ping', count_param, str(ping_count), host]
# When calling subprocess.Popen, we are redirecting stdout and stderr to PIPE
# This will cause the proces to return a tuple of (stdout, stderr) when communicate
# is called
# We do this even if we don't want or need the results, so it doesn't display to
# screen while executing
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = process.communicate()
# Use the Popen variable (i.e. process) to get the return code. The output from the
# command is held in result, as described above.
# In this example, we are only looking at stdout. We could also get stderr as result
# if that were interesting to us.
return (process.returncode, result)
# This function is just to get command line arguments, and demonstrate how to call
# The ping_test function.
args = sys.argv[1:]
if len(args) < 1:
print 'Usage: %s host-or-ip [count]' % sys.argv
return_code, result = ping_test(*args[:2])
print 'Ping Successful' if return_code == 0 else 'Ping Failure'
# Standard boilerplate to call the main() function.
if __name__ == '__main__':