Skip to main content
deleted 496 characters in body
Source Link
starikcetin
  • 5.3k
  • 4
  • 26
  • 60

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.

Listed in this answer are the fundamental ways of handling this situation. Although, 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 script for holding data only

You can create a static script to hold data only. Since it is static, you don't need to assign it to a GameObject. You can simply access your data like ScriptName.Variable = data; etc.

  • No instance or singleton required.
  • You can access data from everywhere in your project.
  • No extra code to pass values between scenes.
  • All variables and data in a single database-like script make it easy to handle them.
  • You will not be able to use a Coroutine inside the static script.
  • You will probably end up with huge lines of variables in a single class if you don't organize well.
  • 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;
        }
    }
 
    public static int Deaths 
    {
        get 
        {
            return deaths;
        }
        set 
        {
            deaths = value;
        }
    }
 
    public static int Assists 
    {
        get 
        {
            return assists;
        }
        set 
        {
            assists = value;
        }
    }
 
    public static int Points 
    {
        get 
        {
            return points;
        }
        set 
        {
            points = value;
        }
    }
}

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 usally 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 secene 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 correct scene is loaded to do the job. This causes loads of if-else or switch-case checks.
  • Easy to manage since Unity handles all background process.
  • You can pass data not only between scenes but also between instances (game sessions).
  • Uses file system.
  • Data can easily be changed from prefs file.
  • Slow.
  • Uses file system.
  • Possibility of reading/loading conflicts caused by stream interruption while saving.
  • Data can easily be changed from the file unless you implement an encryption (Which will make the code even slower.)

Singleton pattern is a really hot 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 conditions.

  • Easy to both set-up and use.
  • You can access data from everywhere in your project.
  • All variables and data in a single database-like script make it easy to handle them.

When Unity quits, it destroys objects in a random order. In principle, a Singleton is only destroyed when application quits. If any script calls Instance after it have 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.

Listed in this answer are the fundamental ways of handling this situation. However, 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 class for holding data only

You can create a static class 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 such as ClassName.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.
  • All variables and data in a single database-like class make it easy to manage them.
  • You cannot use a Coroutine inside a static class.
  • You will probably end up with a huge class if you don't organize well.
  • Since static classes cannot be assigned to GameObjects, you can't assign fields/variables inside the editor.
public static class PlayerStats
{
    public static int Kills { get; set; }
    public static int Deaths { get; set; }
    public static int Assists { get; set; }
    public static float Points { 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 usually 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 scene 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 may cause you to write lots of long if-else or switch-case statements.
  • Easy to manage since Unity handles all background logic.
  • 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 saving.
  • Data can easily be changed from the file unless you implement encryption, which will make the code even slower.

Singleton pattern is a really controversial 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 needs and scale.

  • Easy to both setup and use.
  • You can access data from everywhere in your project.
  • All variables and data in a single database-like class make it easy to manage them.

When Unity quits, it destroys objects in random order. In principle, a Singleton is only destroyed when the application quits. If any script calls Instance after it has 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.

Commonmark migration
Source Link

1. A static script for holding data only

#1. A static script for holding data only YouYou can create a static script to hold data only. Since it is static, you don't need to assign it to a GameObject. You can simply access your data like ScriptName.Variable = data; etc.

###Pros:

Pros:

###Cons:

Cons:

###An Example:

An Example:

2. DontDestroyOnLoad

#2. DontDestroyOnLoad IfIf 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 usally the way to go for this).

###Pros:

Pros:

###Cons:

Cons:

3. PlayerPrefs

#3. PlayerPrefs YouYou can implement this if you also want your data to be stored even if the game gets closed.

###Pros:

Pros:

###Cons:

Cons:

4. Saving to a file

#4. Saving to a file ThisThis is a bit overkill for storing values between scenes. If you don't need encryption, I discourage you from this method.

###Pros:

Pros:

###Cons:

Cons:

5. Singleton pattern

#5. Singleton pattern Singleton pattern is a really hot 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 conditions.

###Pros:

Pros:

###Cons:

Cons:

#1. A static script for holding data only You can create a static script to hold data only. Since it is static, you don't need to assign it to a GameObject. You can simply access your data like ScriptName.Variable = data; etc.

###Pros:

###Cons:

###An Example:

#2. DontDestroyOnLoad 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 usally the way to go for this).

###Pros:

###Cons:

#3. PlayerPrefs You can implement this if you also want your data to be stored even if the game gets closed.

###Pros:

###Cons:

#4. Saving to a file This is a bit overkill for storing values between scenes. If you don't need encryption, I discourage you from this method.

###Pros:

###Cons:

#5. Singleton pattern Singleton pattern is a really hot 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 conditions.

###Pros:

###Cons:

1. A static script for holding data only

You can create a static script to hold data only. Since it is static, you don't need to assign it to a GameObject. You can simply access your data like ScriptName.Variable = data; etc.

Pros:

Cons:

An Example:

2. DontDestroyOnLoad

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 usally the way to go for this).

Pros:

Cons:

3. PlayerPrefs

You can implement this if you also want your data to be stored even if the game gets closed.

Pros:

Cons:

4. Saving to a file

This is a bit overkill for storing values between scenes. If you don't need encryption, I discourage you from this method.

Pros:

Cons:

5. Singleton pattern

Singleton pattern is a really hot 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 conditions.

Pros:

Cons:

added 377 characters in body
Source Link
starikcetin
  • 5.3k
  • 4
  • 26
  • 60

Listed in this answer are the fundamental ways of handling this situation. Although, 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 script for holding data only You can create a static script to hold data only. Since it is static, you don't need to assign it to a GameObject. You can simply access your data like ScriptName.Variable = data; etc.

#1. A static script for holding data only You can create a static script to hold data only. Since it is static, you don't need to assign it to a GameObject. You can simply access your data like ScriptName.Variable = data; etc.

Listed in this answer are the fundamental ways of handling this situation. Although, 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 script for holding data only You can create a static script to hold data only. Since it is static, you don't need to assign it to a GameObject. You can simply access your data like ScriptName.Variable = data; etc.

edited body
Source Link
starikcetin
  • 5.3k
  • 4
  • 26
  • 60
Loading
added 88 characters in body
Source Link
starikcetin
  • 5.3k
  • 4
  • 26
  • 60
Loading
added singleton patten to the list.
Source Link
starikcetin
  • 5.3k
  • 4
  • 26
  • 60
Loading
added 23 characters in body
Source Link
starikcetin
  • 5.3k
  • 4
  • 26
  • 60
Loading
deleted 254 characters in body
Source Link
starikcetin
  • 5.3k
  • 4
  • 26
  • 60
Loading
Fixed grammar
Source Link
Gnemlock
  • 5.3k
  • 5
  • 28
  • 59
Loading
added pros and cons; added 3 characters in body
Source Link
starikcetin
  • 5.3k
  • 4
  • 26
  • 60
Loading
added 4 characters in body
Source Link
jhocking
  • 15.8k
  • 2
  • 44
  • 59
Loading
example added
Source Link
starikcetin
  • 5.3k
  • 4
  • 26
  • 60
Loading
fixed a letter missing
Source Link
starikcetin
  • 5.3k
  • 4
  • 26
  • 60
Loading
Source Link
starikcetin
  • 5.3k
  • 4
  • 26
  • 60
Loading