load Json datas into a static class to store character datas

Hello Guys,
I am stuck about how to do well things arround Json.

I have a static class to store my character datas (name, attaque, level etc.)
I have a class to load JSON datas coming from a php script.

My problem is to store my json datas into a static class to keep it during all the game, but I can’t make it directly :frowning:

Global Class for all project :

public class GlobalVar {
public static CharacterJson characterJson = new CharacterJson();
}

My Character class, that is not a static one to load easly from JSON

[System.Serializable]
public class CharacterJson {
    public string name;
    public int attaque;
    public int level;
}

JSON load correctly my datas

GlobalVar.characterJson = JsonUtility.FromJson<CharacterJson>(JsonDataFromServer);
info.text = GlobalVar.characterJson.name;

info.text display the data correctly, but when i will use my Character class elswhere i got a Null.that’s why i am looking to store my Json directly in a static Character class.

I dont want to create a “copy” static class to pass data. like :

public static class Character {
    public static string name;
    public static int attaque;
    public static int level;
}

I want to avoid a thing like that in my script, especialy if i must to change my Character class to add more datas.

Character.name = GlobalVar.characterJson.name;
Character.attaque = GlobalVar.characterJson.attaque;
Character.level = GlobalVar.characterJson.level;

I need to know how to put my datas from Json directly in my static class.

Well i hope i am clear enought.
Cheers

Can you show how you are accessing the data? What you shared seems like it should work, as long as things are done in the correct order (data is loaded before you access it).

Your are right, this point is fixed by acessing the data after its loaded :slight_smile: …basic things but… well thanks !!

And now the main point, what can i do to load my Json data direclty in my static Character class without doing that way => for all variables… Character.name = GlobalVar.characterJson.name; etc…

You can’t really. I’m not sure what advantage the static Character class would be giving you.

Hi, good point again :slight_smile:
Well thanks a lot you helped me to understand why i where stuck and how to work arround. i can go further now, this is great, you make my day ^^