Listed in this answer are the fundamental ways of handling this situation. AlthoughHowever, most of these methods do not scale well to large projects. If you want something more scalable and are not afraid of getting your hands dirty, check out the answer by Lea Hayes about Dependency Injection frameworks.
1. A static scriptclass for holding data only
You can create a static scriptclass to hold data only. Since it is static, you don't need to assign it to a GameObject (i.e. you don't need to create an instance of it). You can simply access your data likesuch as ScriptNameClassName.Variable = data;
etc.
- No instance or singleton is required.
- You can access data from everywhere in your project.
- No extra code to pass values between scenes scenes.
- All variables and data in a single database-like scriptclass make it easy to handlemanage them.
- You will not be able tocannot use a Coroutine inside thea static scriptclass.
- You will probably end up with huge lines of variables in a singlehuge class if you don't organize well.
- YouSince static classes cannot be assigned to GameObjects, you can't assign fields/variables inside the editor.
public static class PlayerStats
{
private static int kills, deaths, assists, points;
public static int Kills
{
get
{
return kills;
}
set
{
kills = value;
}
get; set; }
public static int Deaths
{
get
{
return deaths;
}
set
{
deaths = value;
}
get; set; }
public static int Assists
{
get
{
return assists;
}
set
{
assists = value;
}
get; set; }
public static intfloat Points
{
get
{
return points;
}
set
{
points = value;
}
get; set; }
}
If you need your script to be assigned to a GameObject or derive from MonoBehavior, then you can add DontDestroyOnLoad(gameObject);
line to your class where it can be executed once (Placing it in Awake()
is usallyusually the way to go for this).
- You will probably need to adjust your scene depending on the script.
- You will probably need to check which secenescene is loaded to determine what to do in Update() or other general functions/methods. For example, if you are doing something with UI in Update(), then you need to check if the correct scene is loaded to do the job. This causes loadsmay cause you to write lots of long if-else or switch-case checksstatements.
- Easy to manage since Unity handles all background processlogic.
- You can pass data not only between scenes but also between instances (game sessions).
- Uses file system.
- Data can easily be changed from the prefs file.
- Slow.
- Uses file system.
- Possibility of reading/loading conflicts caused by stream interruption while while saving.
- Data can easily be changed from the file unless you implement an encryption (Which, which will make the code even slower.)
Singleton pattern is a really hotcontroversial topic in object-oriented programming. Some suggest it, and some don't. Research it yourself and make the appropriate call depending on your project's conditionsneeds and scale.
- Easy to both set-upsetup and use.
- You can access data from everywhere in your project.
- All variables and data in a single database-like scriptclass make it easy to handlemanage them.
When Unity quits, it destroys objects in a random order. In principle, a Singleton is only destroyed when the application quits. If any script calls Instance after it havehas been destroyed, it will create a buggy ghost object that will stay on the Editor scene even after stopping playing the Application. Really bad! So, this was made to be sure we're not creating that buggy ghost object.