Beware of functions in disguise

C# properties are great language features for wrapping up access to variables through internal functions. This helps us validate values assigned to variables, and to detect when variables change. However, C# properties have a performance overhead that makes them expensive compared to direct variable access. For this reason, to optimize script performance, always seek to cache variables. This problem is most notable in Unity when using MonoBehaviour class variables, or static class variables, which provide shortcut access to other components or objects. Consider the following example:

transform.position = new Vector3(0,0,0);

Here, the transform variable provides syntax-quick access to the Transform component ...

Get Mastering Unity 2017 Game Development with C# - Second 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.