less than 1 minute read

C# 9 introduced the init keyword which can be applied to properties and indexers. This means the value can only be set during object initialization (unlike set). Unity supports C# 9 since 2021.2, but you’ll notice the init keyword doesn’t work right out of the box.

Image Click Area

The compiler complains about missing something called IsExternalInit. If you search online, you’ll find a small snippet of code that you can add to your project to fill in the missing piece.

Image Click Area

Of course, MonoBehaviours can’t benefit from this feature, but still a lot of use cases. Immutable properties ftw!

init keyword docs

IsExternalInit snippet

Image Click Area

Comments