V3212. Unity Engine. Pattern matching does not correctly handle destroyed objects derived from 'UnityEngine.Object'.
The analyzer has detected a potential error related to the object derived from UnityEngine.Object
. The object was used with pattern matching to check for null
.
According to the Unity documentation, it is not recommended to use pattern matching with objects derived from UnityEngine.Object
.
The check for null
using pattern matching does not consider that objects derived from UnityEngine.Object
may be destroyed. Therefore, the check may have an incorrect result.
Look at the example:
class CustomComponent: MonoBehaviour
{
public bool HitConfirm(....)
{
Projectile bullet;
....
if(....)
Destroy(bullet);
if (bullet is not null)
{....}
}
}
In this case, the check of bullet
using pattern matching will not consider that the object may be destroyed.
To fix this, use a check with the ==
or !=
operator.
The fixed code:
class CustomComponent: MonoBehaviour
{
public bool HitConfirm(....)
{
Projectile bullet;
....
if(....)
Destroy(bullet);
if (bullet != null)
{....}
}
}