- Home /
Question about classes C#
Ok, so I am trying to make a 2d rpg kinda akin to final fantasy with a better battle system. However, I am having trouble trying to figure out how to get my character classes between scenes. So I tried to make a static class with public variables to find out I cant do that. So here is what I need.
I need a class to hold variables that all relate to one character
I need to be able to change them from other scripts
I need to be able to access them across different scenes
What would be the best way to go about doing this?
Answer by CHPedersen · Aug 18, 2014 at 07:48 PM
Your class should be a script you put on your player gameobject. In the Start method or Awake method of this script, you must call DontDestroyOnLoad which is exactly meant to preserve a gameobject including all its components through scene changes, thus accomplishing this.
Making everything static is an oft-chosen yet poor design choice for inter-scene persistence. Those variables that constitute defining characteristics about your player character should instead be made public properties, which are implemented like so, in C#:
public int MyProperty { get; set; }
With an int for this example, though the type could be anything. The "get" and "set" parts afterwards ensure external code can read and set this variable. Properties like the int in this example are the standard way to accomplish these things in C#, but beware that they are not serialized in Unity, so they will not appear in the editor. It's possible to get them to do so, but if that is a requirement, the code must be slightly more involved, as you will have to declare the underlying variable exposed through the property yourself and make sure it is serialized. Such an implementation would look like this:
public int MyProperty { get { return _myPropertysInt; } set { _myPropertysInt = value; } }
[SerializeField]
private int _myPropertysInt;
Thanks! Didn't know about the DontDestroyOnLoad. I will use that.
No I won't be using them in the editor. I only need to reference them in the code, sorry should have specified that. Basically all it is doing is holding my stats like hp, attack, etc. So I will get them from a save file, or define them if you start a new game.
You're welcome. :) In this case, the auto-implemented property (the top one) will suffice. The only gotcha you have to beware of with DontDestroyOnLoad is that you don't revisit the scene that contains the original player gameobject without manually Destroy'ing the player. If you do, you'll get dublicate player objects, because the first one travelled along with you through all scene changes.
Thanks! That is nice to know because I am going to be carrying the stats over to battle, and after the battle going back to the first scene. So maybe it would be better to create them after the title screen, and by default none of my scenes have him, but since he isn't destroyed when loading he will carry over.
Your answer
Follow this Question
Related Questions
How do you make RPG class stat presets? 1 Answer
Creation of RPG Class Stat Presets? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# List and GUI 3 Answers