Random question about getcomponent

So I know its often recommended to keep scripts performing different operations as separate scripts for organizational reasons. My question is why, being that it would make it more likely you would have to use functions like getcomponent which is not only costly but also tedious to do especially from different objects. Is it recommended to just use your own discretion? Opinions are welcome or any tips and advice to a novice!

I would say the benefits of the component model are:

  • Reusable modules of code
  • Avoiding code duplication
  • Ability to develop complex behavior by building it out of smaller parts
  • Loose coupling between code modules

Working with components is like building with Lego. Each individual brick doesn’t do much and isn’t very sophisticated. But when combined with other bricks then something wonderful emerges. You can take the whole thing apart and make something different or add new bricks to give the creation new behavior (wheels, wings etc…).

For example consider AI code to drive a car. If you do it right you should be able to apply the same AI behavior to different vehicle rigs by just adding the AI components to the new car game objects. If you don’t like the AI behavior then you remove the AI components and add different AI components, perhaps something purchased from the asset store. When developing your next game it is easy to re-use the AI scripts from your previous game. You could sell the AI scripts as a package in the asset store.

When working with one monolithic script per game object then it is MUCH harder to reuse code and avoid duplication.