Saving Variables to be used for next scene.

Hey guys, I’m new to C# and unity since I’ve made a transaction from AS3 and Flash.

What I currently have is a “PlayerStats” class (which maybe used for enemies too in the future)

This class just has these two variables at the moment : attack, and defense.

I have another separate class called “StartGame” attached to my main camera in the scene. When I start the game it will prompt the user to add points in to attack and defense. (ps.attack ++) etc, and to enter a name too.

When there is a name and no points left to add on or subtract, the start button becomes enabled and goes to my new scene.

My question is. lets say the player had 15 attack points, and 5 defense points. How can I load that to another class? Or can I use my “StartGame” class again with the saved variables and add it to my player.

Sorry if this is too much, if any of it does not make sense, please do say so.

Thank you.

There are two possibilities that come to mind:

Thank you for the quick post.

I will look into this and report back to you.

Use DontDestroyOnLoad to preserve an object from one scene to the next. Dont destroy your StartGame object, and use it to create/edit your player upon reaching the next scene.

Hey, I used getters and setters

public class playerStats  {

    public  int attack;
    public  int defence;
    public  int stamina;
    public  string name;

    public playerStats()
    {
        attack = 5;
        defence = 5;
        stamina = 5;
        name ="";
    }

    public int MyAttack {
        get { return attack; }
        set { attack = value; }
    }
    public int MyDefense{
        get { return defence; }
        set { defence = value; }
    }
    public int MyStamina {
        get { return stamina; }
        set { stamina = value; }
    }
    public string myName {
        get { return name; }
        set { name = value; }
    }
}

and i made an instance of this class, but it didn’t set the attack value to 15. I tried making the variable static, but it didn’t allow me to change the variable.

I’ve tried using “DontDestroyOnLoad” what it does is uses the same code, my create character code is on one file, which means it will appear two times.

Thanks though:smile:

Static variables cant be changed in the inspector. I’m not sure what you are trying to accomplish with the getters/setters or a static variable. You state that you need to have data persist between scenes. This means using DontDestroyOnLoad for whatever object holds your data (I recommend moving StartGame script to a separate gameobject than the camera, as you dont need the camera to persist). OR you can implement some sort of save/load feature. PlayerPrefs is the most basic, but can get cumbersome if you start using it for a lot of data.

This is what you would need if you decide to use static variables to retain values across scenes.

Modified Script

using UnityEngine;
using System.Collections;

public class playerStats  {
  
    public static int attack;
    public static int defence;
    public static int stamina;
    public static string name;
  
    public playerStats()
    {
        attack = 5;
        defence = 5;
        stamina = 5;
        name ="";
    }
  
    public static int MyAttack {
        get { return attack; }
        set { attack = value; }
    }
    public static int MyDefense{
        get { return defence; }
        set { defence = value; }
    }
    public static int MyStamina {
        get { return stamina; }
        set { stamina = value; }
    }
    public static string myName {
        get { return name; }
        set { name = value; }
    }
}

But please keep in mind static variables aren’t available in the inspector, making them cumbersome to track and modify. I’d probably recommend PlayerPrefs - instantiating objects with the PlayerPref values in the Awake method then saving the PlayerPref values in the OnDisable method.

Also, as mentioned, DontDestroyOnLoad is another possibility, but you’ll need to do your tests starting from level where the object is originally created.

Thank you for your answers and explanations.

I’ve chosen to use DontDestroyOnLoad, despite the testings, since I want to persist the data from one scene to another.

Scene 1 - Player chooses stats - Scene 2 - game loads with the stats that have been chosen from scene 1

This isn’t possible with static variables since you can’t change them :frowning:

Actually you can modify static variables from different scenes.

The static doesn’t refer to the value, but rather the memory allocation. It’s statically allocated for as long as the program runs.