Remove Empty Unity Events - Do Empty Unity Events cause Performance Issues?

      One thing you have noticed is, whenever you create a new script in Unity3D, the Unity scripting template adds two methods or unity events by default. The two unity methods are Start () &Update ().
using UnityEngine;
using System.Collections;

public class MyCustomScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

The good thing about the default script template is that you can edit it. You can find the Unity Script template file at this location %EDITOR_PATH%\Data\Resources\ScriptTemplates
where EDITOR_PATH is the actual unity installation location in your PC (watch below video learn how to edit unity default script template).
You can edit this script template according to your requirements.
A common question asked by most of the unity developers or users that Do Empty Unity events (e.g., start, update or awake, etc.,) create any performance issue or impact? The simple answer is YES! After watching the optimization video of Unity 2020 and my personal experience I can confirm this thing to you.  Actually, the empty events incur a small overhead. If you have fewer scripts with empty unity events then maybe it will not make any significant difference but as your project grows and you have  Hundred or thousand of empty unity events then, certainly it will cost performance issues. So, the best practice is to remove those events when you don't require it. And if you required those events for a specific platform like for testing purposes you can use unity platform dependent compilation like this:
#if UNITY_EDITOR
void Update(){

}
#endif
Now, the Update()code block will only run in the editor and it will not work or compile in the final build. Or you can edit the default scripting template file according to your needs which I have mentioned in the above video.  
One more important thing I want to share with you is that always prefer 1 update method over several update methods when you are doing same kind of task. For example, you are scaling a number of objects based on camera distance on the Update event. You can write and attach one script to several objects with your code logic that will be executing in the Update method. Instead, I will suggest you to make a list of all objects and scale it using single Update method.

Happy coding!

Post a Comment

0 Comments