Optimizing your Code

bg7

When it comes to scripting, even at an intermediate level, there are a few things that you can do to make sure that your code is lean and efficient, including:

  • Using Update and FixedUpdate wisely.
  • Deactivating unnecessary scripts temporarily.
  • Removing overridable methods.
  • Using search functions wisely.


Using Update and FixedUpdate

Some functions like FixedUpdate or Update can be called several times per second; so it is good practice not to process too much in these, unless necessary, as it would slow down your game otherwise. The former (FixedUpdate) can be called 10 times per second, while the latter (Update) is called every time your screen is refreshed. So, it’s better not to add code in these, unless necessary; and if you really have to use one of these methods, then the Update method may be best.

So the following code…

public void Update()

{

print(“Starting the game”);

}

could be replaced by this code.

public void Start()

{

print(“Starting the game”);

}

This is because the code will only be called once (at the start of the game) and that it does not to be used every frame.

Deactivating scripts that are not used

In addition to choosing in which method to perform actions, it is also good to deactivate the scripts that may not be used temporarily; for example, some objects linked to NPCs may not be used until the player gets close to these NPCs; in this case, you can deactivate the script after creating a reference to them from the player for example.

For example, let’s imagine that this script called NPC.cs was linked to an NPC.

void Start()

{

GameObject player = GameObject.find("player");

}

void Update()

{

if (Vector3.Distance(gameObect.transform, player.transform) <= 1.0f) Fire();

}

public void Fire()

{

...

}

You could activate the script as follows:

void Start()

{

GameObject NPC = GameObject.find("npc");

NPC.GetComponent().active = false;

}

void Update()

{

if (Vector3.Distance(gameObect.transform, NPC.transform) < 1.0f) ActivateNPC();

}

public void Fire()

{

NPC.GetComponent().active = false;

}

Removing overridable methods

As you may have already noticed, new C# files in Unity usually extend the MonoBehaviour class, which means that the new class inherits some properties from the base class (MonoBehaviour), including methods that can be overridden.

This is the case, for example, for the methods Start and Update. Now, when you create a new C# script, it usually includes an empty Update method by default;

Update (){}

It is instinctive to think that because it is empty it does not do anything; however, the issue is that, whenever your script includes an Update method, it is added to a list of script to be called every frame, even if the Update method does not include any code. So the good thing to do here is to remove any empty “magic” method from your code (e.g., Start, OnAwake, Update).

Using search functions wisely


At this stage of your journey through Unity, you may already have used built-in methods such as <strong>GameObject.Find</strong> to locate and refer to specific objects in your scene; this is a search function that looks up within the scene among all objects within to find a particular object. These search functions are usually costly in terms of time and performance because every time your search for an object, it looks-up through all the objects in the scene to find a particular object, and for this reason, it is good practice to use these as little as possible or at least in methods that are not called often (i.e., especially not in the Update or <strong>FixedUpdate</strong>); this being said, a better way to do use search functions is to look for and “cache” an object once (in the <strong>Start</strong> method for example) and then make a reference to the object found that can be used later without the need to search for the object again or from a costly method, as follows.
<div>

void Start()

{

GameObject NPC = GameObject.find("npc");

}

void Update()

{

if (…) NPC.transform.position ...

}

If you really need to look for an object every frame, you may use the method GameObject.FindWithTag instead which is less computer intensive.


Related Articles:

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>