USP0009 Don't flag methods decorated with the ContextMenu attribute or referenced by a field with the ContextMenuItem attribute as unused
June 17, 2025 ยท View on GitHub
Methods decorated with ContextMenu/MenuItem attribute, or referenced by a field with ContextMenuItem are not unused.
Suppressed Diagnostic ID
IDE0051 - Remove unused private members
Examples of code that produces a suppressed diagnostic
using UnityEngine;
class Camera : MonoBehaviour
{
[ContextMenu("Context Menu Name")]
private void ContextMenuMethod()
{
// Some code
}
}
and:
using UnityEngine;
class Camera : MonoBehaviour
{
[ContextMenuItem(""Reset Health"", ""ResetHealth"")]
public int health;
private void ResetHealth()
{
health = 100;
}
}
Why is the diagnostic reported?
The IDE cannot detect any calls to ContextMenuMethod nor ResetHealth in those classes.
Why do we suppress this diagnostic?
ContextMenuMethod and ResetHealth are called indirectly by Unity, which is undetectable by the IDE.