- Home /
Is there anything wrong with using lots of static classes?
For example:
namespace Balls { public static Ball [] balls;
public static class Balls
{
public static void UpdateBalls (){}
}
}
Another file:
using static Balls;
namespace Player { public static class Player { public static bool PlayerAlive; public static Vector3 position;
public static void UpdatePlayer ()
{
for (int i=0; i<balls.Length; i++)
{
if (position == balls[i].position)
{
PlayerAlive = false;
}
}
}
}
}
Another file:
using static Balls; using static Player;
namespace Main { public static class Main { public static void MainUpdate () { if (PlayerAlive) { UpdateBalls (); UpdatePlayer (); } } } }
There's probably a countdown at Unity headquarters for your post to be visited by that one guy who hates the singleton pattern, but using static classes / singletons is a design choice that you're allowed to use as the developer.
They can simplify interactions with components that are either read- or write-only for a given aspect, like a common Boss UI or updating a the tally of current bullets on screen, but can also make it much easier to cross-cut and write spaghetti. In your case, the use of "using static ___" might introduce reader confusion as to where balls
or PlayerAlive
are co$$anonymous$$g from, so I think those should almost always have the static class name written explicitly before their use to help maintenance / readability.
I'm not sure how relevant this is to Unity and there are a bunch of other questions about the use of static
in a game-development context, so someone else can delete if they want to.
Thank you for your informative response. Basically I watched a few videos from this guy $$anonymous$$ Will on YouTube. He points out many flaws with OOP enforced program$$anonymous$$g. I dont see why I have to instantiate a singleton of a class just to have some shared data available. I am trying to program procedurlaly in c# lol. Or c++ style in c#. If I need a class, Ill make it. Otherwise, I want to treat my data as free.
Static classes with only static functions are no problem. But when they have data, you have to think if you'll ever want a second copy -- maybe you'll need 2 or more ball lists at the same time, or a 2-player game.
Answer by Cornelis-de-Jager · Nov 26, 2019 at 04:40 AM
No.
The advantage of normal classes is if you want to inheritance, or just mutliple classes.
Other than that static classes are fine.