Organizing and cleaning up the Code

With more and more features being added to the game, the code is starting to look a bit too chaotic. Especially the ‘Player’ script - mainly when trying to play around with the values in the ‘Inspector.

Picture from the mighty Internet - Original source: Unknown..’

Well, it is time to do some organizing and cleaning up certain bits and pieces of the code. I will do this in four ways:

  • Cleaning up unnecessary code and comments - Pretty self-explanatory, but I will also be checking for any unnecessary empty(blank) lines and lousy formatting, as well as any unused namespaces.
  • Removing hardcoded values - These values will be replaced with variables, which you will be able to set in the Inspector.
Laser ‘script’ - replacing hardcoded values
  • Adding the ‘RequiredComponent’ - This line of code is beneficial, as it allows you to set specific Components on a GameObject as required for the script to be attached. This also means that you will not be able to remove any of these components by mistake.
‘Enemy’ script - setting up required components.
  • Organizing variables with ‘Header’(s) and ‘Tooltip’(s) - This is used to group your variables under a section by their function. This allows any programmer or a designer to work with values, knowing what they do without having to read the code.
  • Cleaning up the Start() and Update() methods to include as little code as possible, grouping up similar code in separate methods.
  • Adding ‘Regions’ to the code - Regions allow for grouping up methods with similar functions, so unless the programmer needs to work on that function, it can remain closed.
Region set up for the ‘Thrusters.’

Now the code is a little bit more organized and easier to work with! Always try to have your code as clean and as organized as possible so that any new programmer coming to the project, or you returning to the code after years, can understand it as quickly as possible.

But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!



Aspiring and eager beginner Unity developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store