Nerd For Tech
Published in

Nerd For Tech

Tips for Code Optimization in Unity

In the last article, we explained one of the best tools Unity offers which is the Unity Profiler (Unity Profiler 101). The profiler helps you identify which part of your code is actually causing some performance issues. Let’s get to it.

GetComponent

Getting a component of any object is an essential part of unity development, usually you we get hold of a script or a Rigidbody or maybe the material of the object by GetComponent.

Sometimes you get the component without caching it first. For example, say you want to change the color of a cube. You might do the following in Update:

This will get the component of the object EVERY single frame to change the color of the object. If you check this using the profiler, you will see that this will cause some garbage collection and performance issues in the long run. One code may not do much harm, but imagine doing it more than once.

Profiler: the above code causing performance issues

To help improve this, you must always cache your component then find it in Awake or Start and then do whatever you want with the component.

This caching of the component should also be done to variables, always cache to improve performance.

The New Keyword

The “New” keyword, just like getting components, is used to create new variables or Vectors. When used unwisely and very often, it could lead to performance issues.

Let’s say you want to change the position of an object using user input. You might use in update the following:

This will create a new vector each time the user presses the D key. What would be more optimal though, is caching a vector3 for position and then reusing the same vector, like this:

Caching the variables and reusing them instead of the NEW keyword

Yield Return New

Just like the new keyword for variables, IEnumerators require you to yield in order to execute the code inside it.

The yield return needs to create a “new” variable (WaitForSeconds() for example) each time it runs. When you have a lot of coroutines, this will definetly lead to garbage collection and performance issues. Instead of creating a new WaitForSeconds each time, cache it and reuse it just like variables. Here is how:

Efficient CoRoutine

By caching the WaitForSeconds instead of creating a new one in the IEnumerator, you almost get rid of garbage collection created by this coroutine, and in the long run improve your application’s performance.

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Importance of Initial Concept & Vision of a Product Feature — A programmer’s perspective

AppsKit Pro Review +Massive AppsKitPro Bonuses +Discount +OTO Info -Create apps for both iOS &…

What is Progressive Web Apps by Google?

Branch Prediction — Everything you need to know.

A pattern to easily enforce external data validation in Swift

Build Your Webinar, E-Learning or Video Conference System Easily

Is it hard to be a good programmer? All it takes is these 10 habits

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
Mohamed Hijazi

Mohamed Hijazi

A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/

More from Medium

Reverse Geocoding: Unity lambda expression

Unity Development — The Resources Folder

47 High Quality Plugins on Unity Asset Store-Essential Tools for Unity Developers

Code Optimization- Game Dev Series 145