5 Quick Unity Tips

I've recently started reading through the entirety of the online Unity manual. Originally it was due to curiosity, but it has proven to be very helpful because there are a ton of tidbits about the engine that I wasn't even aware existed.

Listed below are the 5 most useful pieces of info I've uncovered so far. Hopefully they can help you on your games as well!


Start as a Coroutine

Even though it's not obvious in the API documentation, Start() can actually be treated as a coroutine. In order to do so, instead of the usual "void Start()" that you would write, make it "IEnumerator Start()". You can then use yields directly in your Start() code. This can make initial animations of things a snap. In our current project, we even use it for initialization. In one of our scripts, we want to perform some setup, and then wait while all the other scripts finish their setup, and then finish ours. How we actually do this is basically like so:

IEnumerator Start()
{
// Perform initial setup
yield return null;
// Perform setup after other scripts have done theirs
}

NOTE: As u/dudledok and u/LHHuman have pointed out, it is a bad idea to initialize things in this manner. Awake() and OnEnable() are usually a better idea for initializing things in steps. This method can still be useful for animating things right off the bat, rather than having to call StartCoroutine(AnimateFunction());


OnDrawGizmos

MonoBehaviours provide an "OnDrawGizmos()" function that can be implemented to draw things in the Scene view while developing. This can be extremely useful for designing and building levels, especially if you have a lot of invisible objects in your scenes.

In our current project, we make extensive use of triggers. They are basically just box colliders, so they are invisible to the player. Unfortunately, this means they are also invisible to us while we are designing, unless we have the objects specifically selected. Using OnDrawGizmos, we can insert the following code to draw a rectangle where the trigger actually is.

Note that this code can be altered to draw pretty much anything you want! And the static Gizmos class offers several other functions to assist with drawing, such as Gizmos.DrawCube, Gizmos.DrawMesh, and Gizmos.DrawSphere.

In the comments below, @ChevyRay offered the following code which does the same thing as above, except more efficiently and it rotates with the collider. Check it out: http://pastebin.com/95fqFAfF


Prefabs for Killing Things

Normally, when something is killed or destroyed, there is a tendency to change a bunch of its properties through code. For instance, turn off its box collider, play a particle effect, play a sound, etc. This can be simplified, and made more easily editable during development, by simply destroying the existing object and instantiating a new one on death (or, as Anon and u/phphelpneeded pointed out, putting the existing object back into its object pool and pulling the death prefab from its pool). So for the above example, you'd make a new prefab with no box collider, a particle effect and an audio source that both auto-play. 

Note that creating/destroying objects is usually less efficient than changing properties directly in code. But it can significantly speed up development time. It can also allow the designers/artists on your team to enhance death effects without touching a line of code.


Animation Curves

Animation Curves + Coroutines = Animated Menus!

So far in my entire gamedev career, nothing has given such huge payoff with such little work as Animation Curves. Coupled with coroutines, I am practically animating everything in our game now. If you don't already use Animation Curves, you owe it to yourself to check out this tweet for a super quick overview: https://twitter.com/DanielJMoran/status/727535792571064321

It really is that simple! All you do is create a public AnimationCurve variable, then call Evaluate() on it wherever you would normally have a linearly changing value. You can then change the AnimationCurve to your liking through the editor.


Random Variables

There are a few useful static Random variables provided by Unity. They are not hugely powerful, but they can save some time and a few keystrokes. For instance, in the past I've used Random.Range(0, 1f) countless times. The shorthand way for that is Random.value. There is also Random.insideUnitSphere, Random.rotation, and a few others. Check out the API docs for the complete list!


That's it for this week! Some of these tips have drastically improved our games, and others have simply sped up our workflow a bit. But as I'm sure every developer can attest to, every little bit helps! Here's to hoping you can use some of this info in your own games.