UNT0019 Unnecessary indirection call for GameObject.gameObject
January 14, 2021 ยท View on GitHub
Unity GameObject has a property gameObject that returns this. This allows calls like GameObject.gameObject.gameObject to occur which is redundant and hinders performance.
Examples of patterns that are flagged by this analyzer
using UnityEngine;
class Camera : MonoBehaviour
{
private void OnTriggerEnter(Collider collider)
{
GameObject original = null;
GameObject duplicate = original.gameObject;
}
}
Solution
Fix expression:
using UnityEngine;
class Camera : MonoBehaviour
{
private void OnTriggerEnter(Collider collider)
{
GameObject original = null;
GameObject duplicate = original;
}
}
A code fix is offered for this diagnostic to automatically apply this change.