Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if just storing data, does using static class as global variable in c# Unity harmfull?

I'm currently developping turn based RPG game with Unity. For storing Player informations, monsters list and items list, I use static class.

public static class GlobalVariables 
{
    public static BaseMonster currentMonster;
    public static BaseCharacter player;
    public static List<BaseMonster> monsterDatabase;
    public static Dictionary<string, BaseItem> itemDatabase;
}

I set this values when game loading by json file than use it into all of my scenes.

I wonder if it is harmful like most people' opinions on global variables?

Note: currentMonster is information that set when player selecting monster and Used when "fight scene" loading, to deside what Monster will load.

like image 377
İhsan Cemil Çiçek Avatar asked May 24 '26 01:05

İhsan Cemil Çiçek


1 Answers

It's not acutally bad or harmfull, but I would suggest using Singelton: https://en.wikipedia.org/wiki/Singleton_pattern This way you only use one static member. Here is an example as well:https://msdn.microsoft.com/en-us/library/ff650316.aspx

like image 191
pasotee Avatar answered May 26 '26 15:05

pasotee