Hey guys,
I'm relatively new to Unity, but I've been programming for some time now. Some friends and I are building a game and I had the idea of making a central manager that has references to certain scripts or objects that are consistent throughout the game to make it easier for us to code. I did this by making a Game Object and adding to it a script, GlobalManager.
Now, the idea here is that instead of having to constantly make references to scripts and find them on the scene, etc., the objects and scripts that aren't instantiated or destroyed in the scene have a static reference from this GlobalManager.
Example:
Let's say our main player is the game object Player and its script is the PlayerScript. Then this GlobalManager script will have variables
public static GameObject player;
public static PlayerScript playerScript;
To the playerpublic var, I will drag the player object and the playerScript will be declared on start
playerScript = player.getComponent();
So why do this? It's basically so from every other class that needs to reference either of these things, I can just simply do
GlobalManager.player;
Rather than having to declare new references to the player or potentially complex getComponent chains for these two things that are, for all intents and purposes, static.
That being said, OOP design principles hate static variables and pretty much every advice on the internet is DON'T USE STATIC VARIABLES.
Is there a particular reason why this would be a bad design style in Unity?
Thanks guys!
↧