Skip to content

Avoid multiple unnecessary property accesses

Matt Ellis edited this page Feb 15, 2019 · 3 revisions

Many Unity components expose data from the native part of the engine as properties, such as Transform.localPosition. Accessing these properties requires a transition into native code, which can be expensive. It is recommended to cache the property value in a local variable on first use, and use the variable for subsequent accesses. This will reduce the overhead of unnecessary transitions to native code.

This inspection will highlight repeated property access and provide an Alt+Enter Quick Fix to introduce a local variable, assign it the value of the property and replace subsequent property accesses with the local variable. The inspection will only replace usages up to the point that the property is set a new value, or a related property is set (e.g. Transform.position and Transform.localPosition are related).

This inspection was first added in Rider/ReSharper 2018.3

Clone this wiki locally