How big of a script is too big?

Before context, my question is: Is it better to have one super big script that controls everything your player can do or does, or should I be separating topics into separate scripts (movement, combat, gathering, etc.)

I just started learning Unity and this is the first real coding I have ever done. I have currently just been using one script to put all of my code for my player character (same thing with the enemy I have been making). Before I get too much further, I just want to understand at what point in time should I be creating new scripts to handle specific things that my character does. I can quickly see how even just adding a small feature can add lots of lines of code and different interactions with existing code. Is it better to have that all in one place? Or should this be separated?

Generally speaking the reason that you break a script up into smaller scripts is to reduce your cognitive load, the time to discover problems, and the time to add new functionality. How large of a script are you able to keep in your head and understand where everything is? How about after a week has passed? How about a month?

I like to keep scripts to a single purpose (eg a script that takes input from all the devices that I need to support and makes it available in a unified manner), but that doesn’t mean the script has to be small. I’ve had work projects with glue logic that had thousands of lines of code per class.

1 Like

depends, in terms of maybe reusing the code later, its often easier if you have separated things out, such as input, attacks, stats, etc, it also helps you narrow down issues and update specific chunks if you keep them separate, but, having started in the days where it was linear coding and really unless using more “business like” languages you had 1 big big file of code and that was that… the answer is, while you can locate what you need quickly and without issue… its fine…

I think there have been many debates over is it more efficient to have a game object with 30 small components or one big one… many times, and some wise people have said, unless its causing you a problem, its probably more time and effort to sort it, than its worth. Unless, its an issue, which will go away or be more managable…

Chances are your users arent seeing your code, and as long as your game is working and if its wonky you can fix it fairly quickly, they wont care, what its written in, how you structured it, whether you called everything “thing” or did pascal casing or whatever, no one. Just think of the headaches you might give yourself and try not to leave too much hassle for future you.

1 Like