"System.ArgumentException: Type provided must be an Enum" when using user-defined enum as a default parameter value in UdonSharp
tracked
z4kky_y
Defining a method parameter of a user-defined enum type with a default value causes the UdonSharp compiler to throw during binding.
The same method compiles successfully when the default value is removed.
This issue does not reproduce with Unity/SDK-defined enums (e.g.,
UnityEngine.HumanBodyBones
, VRC_Pickup.PickupHand).Reproduction
using UdonSharp;
public class EnumTest : UdonSharpBehaviour
{
// Compile error
public void TestMethod(TestEnum testEnum = TestEnum.ValueB)
{
}
}
public enum TestEnum
{
ValueA, ValueB, ValueC
}
Actual Behavior
Compile fails with
Assets/Main/Misc/Enum/EnumTest.cs(6,0): System.ArgumentException: Type provided must be an Enum.
Parameter name: enumType
at System.Enum.ToObject (System.Type enumType, System.Int32 value)
at System.Enum.ToObject (System.Type enumType, System.Object value)
at UdonSharp.Compiler.Symbols.ParameterSymbol..ctor (...)
at UdonSharp.Compiler.Symbols.UdonSharpBehaviourTypeSymbol.CreateSymbol (...)
...
(Full message attached below)
The exception is thrown inside ParameterSymbol when UdonSharp tries to evaluate a parameter’s default value using
Enum.ToObject(enumType, value) during compilation.
For user-defined enums, this call receives an argument that is not recognized as a valid enum type for some reason, which causes
System.ArgumentException: Type provided must be an Enum
.Workarounds
Avoid using default parameter values for enums and provide an overload as a pseudo-default instead
public void TestMethod(TestEnum testEnum)
{
}
public void TestMethod() => TestMethod(TestEnum.ValueA);
Log In
This post was marked as
tracked