Optimized code example

The following script is not optimized. Review the script to see what can be done to optimize it. Then, review the information provided after the script:

public class CucumberManager : MonoBehavior {     public static int currentCucumberCount;     Text Cucumber_Count;     public GameObject[] cucumbers;     void Update() {          Cucumber_Count = GetComponent<Text>();          currentCucumberCount = 1;          cucumbers = GameObject.FindGameObjectsWithTag("Cucumber");          Cucumber_Count.text = cucumbers.Length.ToString();          currentCucumberCount = cucumbers.Length;     }}

Hopefully you were able to spot the inefficient, unoptimized component of the script. In the preceding example, all the statements other than the variable declarations occur in the Update() method. Consider ...

Get Getting Started with Unity 2018 - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.