Microsoft.Unity.Analyzers icon indicating copy to clipboard operation
Microsoft.Unity.Analyzers copied to clipboard

Recommend [RequireComponent] when using GetComponent on self

Open SilentSin opened this issue 5 years ago • 2 comments

Problem statement

Using GetComponent doesn't give any indication of what your script actually needs in the Unity Editor. That's one of the reasons I prefer serialized fields, but GetComponent still has its uses.

Proposed solution

If this.GetComponent is used anywhere in a MonoBehaviour script, offer a code "fix" to add a [RequireComponent] attribute to that class.

I wouldn't want a blue underline for it though since the script could simply null check the component and ignore it if not present. Maybe the analyser could detect that it isn't being null checked?

SilentSin avatar Feb 26 '20 23:02 SilentSin

Very Interesting idea. It might be hard to prove that the component is actually required or not, but that's something we could offer as a suggestion which is not very intrusive.

jbevain avatar Mar 06 '20 17:03 jbevain

Automatically adding "help wanted" tag for stale issues identified as good community contribution opportunities

sailro avatar Apr 26 '21 22:04 sailro