For the applications you are writing it probably isn’t a big deal. When you work on larger applications or when you work in a team it will rapidly become a different matter.
As I said:
You are achieving your desired functional result, and you haven’t run into problems with scale yet, so you’re happy. If you keep at software development, though, you will almost certainly run into issues with scale and/or flexibility.
A classic example is working for a client, designing to implement exactly what they asked for in the fastest reasonable time, and then being asked to change it. In my early days that resulted in lots of terrible code because I didn’t want to “over complicate” things, so I took approaches like the ones described here because they were quick. That did indeed result in fast initial turnarounds. But then come the changes…
Lets continue with the health bar example. I might set one up for the player with a few variables straight in my UI class. Quick, easy, done probably in under an hour. Show the client. “That’s awesome. Now I was thinking that at the end of level 3 there’ll be a boss. Can you just apply it to them, too?” Yeah, ok, that’s not too bad, I make a second set of variables, one for the player and one for bosses. Cool.
The client loves it. “This is awesome, man. Can we add it to all of the enemies?” And then they all need different sizes and colours depending on the importance of the enemy and the type of damage they take. And then we want to add a different type of particle effect to each.
Yes, we can indeed keep kludging it all on top of our base UI class. That can get the required functional results. But it isn’t the most efficient way. Consider:
- How much more complex is that class getting?
- Is the complexity leading to changes taking longer to perform?
- Is the complexity leading to increased bug count?
- Do the above effect design of the application?
- Is a programmer required to make changes?
- What happens if you’ve got two programmers who want to work on the UI at once?
- If the rest of your code is similar, what happens if you want multiple programmers to work on it at all?
OO provides a solution to this. It’s called a “Class”.
Unity provides extensions to this solution, namely MonoBehaviours (classes which can be attached to GameObjects and receive scene callbacks) and Prefabs (a pre-configured GameObject we can drop into a scene).
If we use those solutions then instead of playing a time consuming game of stack-a-hack with a monolithic kludge, we instead make a new variation of a Prefab and set a reference to point at it instead of the original.
Also - and this is critical in cross-discipline teams (ie: game development) - you don’t have to be a programmer to reconfigure something. (This is true in many component environments other than Unity, too.) You sure as heck do have to be a programmer to modify the kludge class.
I’m most experienced with the combination of Object Oriented + Component Oriented because I’ve worked in Unity so much and that’s what’s used here, but there are also other paradigms that also offer solutions.