Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Error When Using `record` Types With Unity3d

I'm trying to use a record with Unity3d. The documentation says C# 9 is now supported, but I still get the error:

The predefined type 'System.Runtime.CompilerServices.IsExternalInit' must be defined or imported in order to declare init-only setter.

The documentation states this, but I'm not sure what it means:

The type System.Runtime.CompilerServices.IsExternalInit is required for full record support as it uses init only setters, but is only available in .NET 5 and later (which Unity doesn’t support). Users can work around this issue by declaring the System.Runtime.CompilerServices.IsExternalInit type in their own projects.

like image 636
rhughes Avatar asked Jun 23 '26 08:06

rhughes


1 Answers

The solution is mentioned in this answer. You need to manually define IsExternalInit somewhere in your project, like this:

using System.ComponentModel;
namespace System.Runtime.CompilerServices
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    internal class IsExternalInit{}
}
like image 78
rhughes Avatar answered Jun 24 '26 21:06

rhughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!