- Home /
Most convenient and fast way to persist/access/modify many values from scene to scene? (C#)
I looked for answers, but still at loss here. I have some character generator, which creates then saves player character variables. They are saved into a fresh script "PlayerCharacterData", which is a component of object CharacterData. This object has DontDestroyOnLoad, so it persists to other scenes. After generating character, game loads character data into that script, and when I switch scenes, data is properly loaded.
But now I have this core and I need my numerous UI objects to load data from "PlayerCharacterData" class, so UI can fill all fields, etc. Later whole game will be depending on variable values from "PlayerCharacterData".
This is part of my PlayerCharacterData script:
public class PlayerCharacterData : MonoBehaviour {
void Awake(){
DontDestroyOnLoad (this); // keeps the object when changing scenes
}
// below we have data stored in variables. It was loaded by another script.
public string characterName;
public int characterSpeed;
public List<Weapon>characterInvWeapon;
// etc, more variables x50-70
}
Now here's example of my public class UIPlayerCharacterData. I added a tag to object CharacterData in Unity Editor to make "Find" faster:
public class UIPlayerCharacterData : PlayerCharacterData {
public void NameToCharacterDataUI() {
// this would be required to make it work: PlayerCharacterData playerCharacterData = GameObject.FindGameObjectWithTag("CharacterData").GetComponent<PlayerCharacterData>();
Text_ch_sh_char_name_string.text = playerCharacterData.characterName;
}
// etc, more functions like that x50-70
void Awake () {
PlayerCharacterData playerCharacterData = GameObject.FindGameObjectWithTag("CharacterData").GetComponent<PlayerCharacterData>();
NameToCharacterDataUI();
// etc, calling more functions x50-70
}
}
Problem is, those classes are on different objects. First is component of CharacterData object which persists from 1st scene, second is component of major UI panel in 2nd scene. Second class has LOTS of UI fields to fill, just showed you one of those. Each takes data from first class (component on CharacterData object). So there's like 50-70 variables for UI to pull data from.
And that's just a start, because whole game will need to get and modify data in PlayerCharacterData script.
Now, I've found out the function NameToCharacterData() isn't working, because it has no reference in playerCharacterSheet variable: "Object reference not set to an instance of an object". Thought it was sorted out in Awake() - I was wrong. So seems we'd need to set PlayerCharacterData playerCharacterData = GameObject.Find("CharacterData").GetComponent<PlayerCharacterData>();
...in every of 50-70 functions filling the fields in UI. Not to mention every other system in game would need to do that too. Plus, everytime player opens the UI those UI fields need to be re-generated.
Is there some faster, more convenient way to do it? I was thinking about making all PlayerCharacterData variables static - this would be fast as lightning, but I'd prefer to make our engine ready for multiplayer (between 1 and 200 players). So not sure.
Also, wouldn't it be better to save character data variables into some script without Monobehaviour, so not connected to a gameObject, and maybe then we could just use PlayerCharacterData playerCharacterData = new PlayerCharacterData();
to get values from that class? I simply need those values accesible really fast and from everywhere.
Really considering the use of static variables at this point, but if there's a better solution, or if there's a better way create static variables for a multiplayer game, let me know.
Your answer
Follow this Question
Related Questions
Saving build number to a file inside build data folder 2 Answers
How to pass data through scenes? 0 Answers
Transfer data between scenes + saving it C# 1 Answer
Multiple Cars not working 1 Answer
Persistence of saving data not working 2 Answers