- Home /
Using a static class & pass in gameObject or just add same script to multiple objects
Hello,
I've been haphazardly self-teaching myself Unity recently, and I have been wondering. I have various scripts that do very broad things that are used in a lot of object (for example, one script has a set of animations like fade from a to b, move from x to y, rotate from m to n all over a certain amount of time) and I can apply the script to the object and call a method to start the coroutine which does the animation.
I was wondering if it would be bad practice to, instead of adding the script to a bunch of objects, declare it as a static class and add a GameObject as a parameter in each method. This way I wouldn't have to worry about adding the script to the object, i could instead just call something like
AnimationScript.moveTo(new Vector3(5, 5), 10.0f, this);
Thanks!
Answer by hiddenspring81 · Jun 01, 2013 at 01:48 AM
I can understand the argument for each approach. Generally, I think that static classes (especially singletons) are things that should be avoided, since it tends to produce poorly encapsulated, tightly coupled code. This can be real headache to maintain once your application becomes fairly complex.
That said, I don't think you should always avoid static classes. I just prefer to avoid static classes that are stateful. If I have a group of related, helper methods, I see nothing wrong with organizing them as part of a static class.
Welcome to Unity answers! If this was helpful, remember to mark it as the accepted answer.