Init Keyword in Unity
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.
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.
Of course, MonoBehaviours can’t benefit from this feature, but still a lot of use cases. Immutable properties ftw!
Comments