In the following article I’m gonna cover a lot of learnings from my last years as Unity programmer which everyone should know and be aware of. Have fun!


1. You want a variable to be exposed to be in Editor but it shouldn’t be declared public because that would suck? Just do that:

[SerializeField] private int hello;

Or you want to serialize a value to keep it but not show it?

[SerializeField, HideInInspector] private int hello;


2. You’re getting random garbage spikes during gameplay and you have no clue why? To be fair, that could be anything! But common things you should look for:

Linq

What can I say!? Don’t use it or whenever you use it, you have to profile. I know it’s convenient but it is a heavy library when it comes down to garbage allocation.

Creating instances of a class

That’s an easy one. Try to always move new calls to the start of your application by adding an Init() method and call it whenever required. You can’t avoid allocating memory, but you can time it so that it doesn’t create spikes during gameplay. I mean in the end, for these kind of things is the loading screen for, right?

Another quick tip within this topic is, that if you pass a function pointer in a method which expects a System.Action to be passed, it converts it implicitly and creates garbage as well! Implicit conversion people miss often times nowadays when it comes down to garbage allocation.

List resizing actions

Everytime you add an element to a List, a Dictionary, an Action or where something can be appended, it has the potential to execute an internal resizing action. Let me explain you this. Whenever you create a dictionary like this:

Dictionary myDictionary = new Dictionary();

it preallocates a default amount of objects. But whenever your Add() method call exceeds the amount of space it preallocated, it has to allocate more. And what most people are not aware of, it literally doubles the amount of allocated space. So 8 becomes 16. To avoid that just do that instead:

Dictionary myDictionary = new Dictionary(10);

That’s it! Do a sophisticated guess on what the maximum amount could be and put it into the constructor. If you want to be 100% sure, you can even create a little script which observes the dictionary and tells you the max amount of ever needed slots during runtime. That you should do for for other data structures as well. But e.g. Actions or IEnumerators, there you have to create a workaround unfortunately.


3. You’re getting a freaking IndexOutOfBoundsException and you have no clue why? That usually happened to me when I did something like this

Whenever you call myAction now, it will possibly fail. Most definitely. So what happens is that the referenced value “i” is not saved by value (so it doesn’t create a copy) but it keeps a reference. That means, the value of i in the end is myArray.Length which is out of bounds.  What you need to do instead is this:

Save the value of i once again, even if it makes things a bit confusing. Then use this (mySavior in our case)  instead of i directly and all is good.


4. You want to serialize a generic class and it just won’t work? There’s nothing I can do for you actually. The only hint I can give you is inheritance might solve your problem.

[Serializable]
class myNewClass : myOldClass<MyType>

For that case you need to specify the type in order to make serialization work. So you lose the freedom to some extent but you win serialization. Sure, this doesn’t work for sealed classes. But there’s nothing I can do for you other than telling you, please reimplement the generic List class again!


5. You have an auto-property and you want to give it a default value? Easy, just do this:

[System.ComponentModel.DefaultValue(“myDefaultValue”)]

public string myVariable {get;set;}


6. You feel like the maintenance of assets is taking you way to much time to set everything up for asset bundles or just a well performing game? Please try Unitys Assetimporter classes. They are amazing. You safe everyones time by introducing good naming conventions which in the end trigger proper import settings for texture compression, animation optimization,target assetbundle definition, mesh optimization and sooo much more. Once setup, everyone will thank you from the bottom of their hearts.

https://docs.unity3d.com/ScriptReference/30_search.html?q=importer


7. Profiling is too confusing or too time consuming for you? Please have a look at the profiling API from Unity. It allows you things like labeling samples, read the heap size or memory allocation. A lot of useful tools to make your life easier.

https://docs.unity3d.com/ScriptReference/Profiling.Profiler.html


8. You find it physically impossible to pause the editor in the perfect moment for you? Just do that:

UnityEngine.Debug.Break();