UNT0039: do not report GetComponent inside editor-only messages#478
Merged
sailro merged 1 commit intoJul 17, 2026
Merged
Conversation
GetComponent calls in Reset/OnValidate are typically used to populate serialized fields with default references that a user can override, so the component is not a hard requirement (microsoft#453). Reuse the editor-only message detection already used by UNT0040 by moving it to SyntaxNodeExtensions.
Contributor
Author
|
@microsoft-github-policy-service agree |
Member
|
Thank you for this contribution ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #453
Why
UNT0039 suggests
[RequireComponent]whenever a MonoBehaviour self-invokesGetComponent<T>(). A common editor pattern is to populate a serialized field with a default reference insideReset(), letting users override it in the inspector. In that context the component is not a hard requirement, so suggesting[RequireComponent]is misleading.What
GetComponentinvocations made inside editor-only Unity messages (Reset,OnValidate).GameObjectIsStaticAnalyzer(UNT0040) is moved toSyntaxNodeExtensionsand shared by both analyzers; no behavior change for UNT0040.Reset/OnValidate; aReset(int)overload (not a Unity message) still reports. Doc page updated with a non-flagged example.Notes
The issue mentions
Reset;OnValidateis included because it is the same editor-only prefill context and the existing editor-only message pair already models it. Happy to narrow this toResetonly if preferred.