AmbiguousMatchException in Missing Methods check when a UnityEvent targets an overloaded method

Open Beginner friendly
#1 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
csharp, unity
Domain
devtools

Research direction

Start in Editor/Categories/MissingReferences/MissingReferencesScanner.cs at ScanUnityEventReferences, around line 257, and inspect how the UnityEvent target method is resolved. Confirm the Missing Methods scan completes for overloaded methods such as AudioSource.PlayOneShot and does not treat a valid persistent call as missing.

Written by the indexing model from the issue text.

Description

Summary

The Missing Methods check in the MissingReferences category throws System.Reflection.AmbiguousMatchException whenever a scanned asset contains a UnityEvent persistent call whose target method has multiple overloads on the target type.

Image

Steps to reproduce

  1. In any project, wire a UnityEvent on any component to AudioSource.PlayOneShot(AudioClip) (e.g. a Button onClick or an XR interactable event playing a UI/select sound). Save the prefab or scene.
Image
  1. Open Unity Scanner and enable the <Missing> Methods check.
  2. Run the analysis.
    The scan of the MissingReferences category fails with:
System.Reflection.AmbiguousMatchException: Ambiguous match found.
  at System.RuntimeType.GetMethodImplCommon (...)
  at System.RuntimeType.GetMethodImpl (...)
  at System.Type.GetMethod (System.String name, System.Reflection.BindingFlags bindingAttr)
  at UnityScanner.Categories.MissingReferences.MissingReferencesScanner.ScanUnityEventReferences (...) MissingReferencesScanner.cs:262
  at UnityScanner.Categories.MissingReferences.MissingReferencesScanner+<ScanAllAssets>d__0.MoveNext () MissingReferencesScanner.cs:156
  at UnityScanner.Categories.MissingReferences.MissingReferencesCategory+<Scan>d__27.MoveNext () MissingReferencesCategory.cs:38
  at UnityScanner.Core.Categories.UnityScannerOrchestrator+<RunCategory>d__16.MoveNext () UnityScannerOrchestrator.cs:114

Root cause

ScanUnityEventReferences verifies method existence with:

var method = resolvedType.GetMethod(methodName,
    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
if (method == null)
    refsData.MissingMethods.Add(new MissingMethodEntry(typeName, methodName, i));

The Type.GetMethod(string, BindingFlags) overload throws AmbiguousMatchException by design when more than one method with that name exists on the type. AudioSource.PlayOneShot has two overloads (PlayOneShot(AudioClip) and PlayOneShot(AudioClip, float)), so the call throws instead of returning a match.

https://learn.microsoft.com/en-us/dotnet/api/system.type.getmethod?view=net-10.0#exceptions

The exception is thrown from here
https://github.com/AlexeyPerov/Unity-Scanner/blob/4baafb0808fc758c45561c79a691c05bbe58c7c1/Editor/Categories/MissingReferences/MissingReferencesScanner.cs#L257

Thanks

Dominant language
C#
Stars
6
Forks
1
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.