Skip to content

Avoid null comparisons against UnityEngine.Object subclasses

Vladimir edited this page Dec 7, 2018 · 3 revisions

The Mono and IL2CPP runtimes treat instances of classes that derive from UnityEngine.Object in a specific way. Invoking methods on the instances actually calls into engine code, which must perform lookups and validations to convert the script references to the native references. While small, the cost of comparing a variable of this type to null is much more expensive than a comparison against a purely C# class.

For this reason, avoid these null comparisons in tight loops or in code that runs every frame.

This inspection will highlight null comparisons against UnityEngine.Object subclasses in performance critical context and provide context action (Alt+Enter) to extract comparison outside the loop and cache value or move comparison to Start or Awake and cache value.

History:

  • This inspection was first added in Rider/ReSharper 2018.3
Clone this wiki locally