Why code to divided script file?

I saw normally, player character has many scripts. 3~7, even more.

Is this because of clarify for human? or for computer?

Is it impossible to code to just 1 script?

An important concept in object-oriented design (and in programming in general) is the ‘single responsibility principle’, which states (in part) that each object or module should have only one responsibility. Application of the SRP has a number of advantages, such as easier code maintenance, increased re-usability, etc.

In practical terms, I think the main reasons for using multiple scripts for a single game object would be:

  1. Better organization and easier maintenance (it’s a little easier to get your head around and modify a script that does only one thing than a long, complex script that does 10 things).

  2. More re-usability (the more an object’s behavior is broken out into separate scripts, the more likely it is that you’ll be able to use those scripts in other contexts as well).

So no, it’s not impossible to code just one script, but in general, that’s probably not the best way to go about it. (Note that I’m not really considering performance here. I suppose in some contexts the overhead associated with running multiple scripts might be a motivation for using a single, complex script, but I don’t really know for sure.)

Thanks for reply.

So concerns I had before I posted this thread and now also,

is that how must I divided my functions to each newly created scripts properly.

Is there no rule for dividing? Just on my mind?

Dividing up your scripts is usually made by common sense and planning ahead. Breaking down what you have to do into manageble related chunks.

The best would be scripts that are self contained and require no access to others. ie. do a simple task
Other end of the scale are ones that rely on looking at 100s of other scripts/objects per frame. ie. debugging nightmare.

If I thinking about Player scripts, I’d plan to have
Main - handles the important stuff.
Movement - handles animation, pathfinding, etc
Weapons - handles firing, and keeping track of ammo.
Inventory - handles picking/dropping/sorting junk.
etc.

Imagine the Inventory has 4 functions: showcontents, hidecontents, additem, removeitem.
If you were developing an RPG game, that script would work for a Crate in your scene. You could click the crate, it opens up a window showing the contents just like a players inventory.

Is nothing wrong with 1 script for a player, but in the long run its just easier to have scripts that do specific tasks, and the more generic, the more reuseablility.

Thanks for postings.

So, I think now it is more good to have as many scripts doing specific things.

I agree that refer other script is very annoying.

So there is balance between 1 script vs 100 scripts at some point.