How to Become a Better Programmer in Unity

With the rise of AI and Machine Learning, it's easy to assume that programmers will not be needed in the future. However, it might take a really long time before Artificial Intelligence is able to interpret more complex tasks, such as abstraction, optimization, interconnection, etc.

Therefore, it's never too late to refine your programming knowledge in Unity, to make yourself not only a more efficient programmer but also, when the time comes, able to supervise the code written by others and guide them in the right direction.

1. Premature optimization is not always the root of all evil

It's a common theme to vilify premature optimization as an unnecessary step when prototyping something, however, it shouldn't be always the case, especially during adding small things, that you know for sure have a great impact on performance, and when it's time to do the real optimization, the time was already saved by taking care of the smaller things. Let's use gameObject.name as an example:

using UnityEngine;

public class ObjectNameScript : MonoBehaviour
{
    public string objectName;

    void Update()
    {
        objectName = gameObject.name;
		Debug.Log(objectName);
    }
}

While using gameObject.name might look harmless, as we discovered in the "Unity Optimize Your Game Using Profiler" post, getting an object's name allocates quite a bit of memory, which multiplies the problem by calling it every frame. So right away, we can move that assignment to the Start function (unless, the object name changes frequently, in that case, it would be more efficient to skip using the name altogether and use a variable instead).

using UnityEngine;

public class ObjectNameScript : MonoBehaviour
{
    public string objectName;

    void Start()
    {
        objectName = gameObject.name;
    }

    void Update()
    {
        Debug.Log(objectName);
    }
}

2. Write shorter code

Writing shorter code always saves time, making it more readable and easier to maintain in the future.

There are many ways to write shorter code, and in the case of Unity, a C# code. For example by simplifying the if/else statement using the '?' symbol:

int numA = 10;
int numB = 5;
int result;
if (numA > numB) {
    result = numA;
} else {
    result = numB;
}
Debug.Log(result);

The same code can be shortened to:

int numA = 10;
int numB = 5;
int result = (numA > numB) ? numA : numB;
Debug.Log(result);

Another way to shorten the code is by putting the repetitive code into its own function and then using that function instead.

3. Code first, ask questions later

While planning is an essential part of any project, if it's a prototype, or if the idea is not yet set in stone, it's important to start coding as soon as possible, since no matter the amount of planning, as soon as you get to work you'll always discover something that can be done better/more efficient, or that some ideas are not as good on practice as others, and vice-versa.

All-in-all, very few things will make you better at programming like getting to work and beginning to write the code.

4. Playtest, before making any changes

When you have a playable demo, it's vital to start testing it as soon as possible to pinpoint where things can be improved, because oftentimes, it may not be apparent just by looking at the code.

Once you master the art of play-testing and collecting feedback, you'll become a more efficient programmer, by knowing exactly where to look to improve the code.

Unity of course provides many useful tools along the way, such as Console (for spotting warnings and errors), Profiler for debugging performance, and the rest of the interface (Scene, Hierarchy, Inspector, etc. for monitoring the flow of the game/project).

5. Optimization is a gradual process

We can all agree that game performance can make or break it and is one of the base pillars of a successful launch. But the game performance can only be assessed by doing the play-tests under various scenarios and conditions, in order to simulate the game from the point of view of regular players.

We have discussed many methods for how to debug, optimize, and find bottlenecks in your game code.

To learn more about how to optimize your Desktop or Mobile game, you can check the posts below:

  1. Unity Optimize Your Game Using Profiler
  2. Improving the Performance of a Mobile Game in Unity 3D
  3. Unity Optimization Tips

If you have your own tips for how to become a better programmer in Unity, feel free to post them in the comments.

Suggested Articles
Working with Arrays and Lists in Unity Code
How to Play Video Files in Unity
How to Add Sniper Scope Effect in Unity
How to Trigger a Cutscene in Unity
Unity C# Must-Know Operators
Unity Obfuscation Methods and Anti-Hack Protection
Implementing Kinetic Interactions in Unity