Forum begins after the advertisement:
[Part 22] struct field initializers is not available in C# 9.0
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 22] struct field initializers is not available in C# 9.0
- This topic has 1 reply, 2 voices, and was last updated 3 days, 11 hours ago by
Alp Apustaja.
-
AuthorPosts
-
December 8, 2025 at 3:47 pm #19105::
Reposting @tengolomidze9190’s question on our Part 22 YouTube video:
Assets\Scripts\Enemy\EnemyStats.cs(11,62): error CS8773: Feature 'struct field initializers' is not available in C# 9.0. Please use language version 10.0 or greater. Assets\Scripts\Enemy\EnemyStats.cs(9,19): error CS8983: A 'struct' with field initializers must include an explicitly declared constructor.do i have to upgrade my unity version to fix this?
btw as i see you are using unity 2020 in this tutorial so how you were able to use C# 10 features?
December 8, 2025 at 3:47 pm #19106::To resolve the errors you are encountering, you may need to upgrade your Unity version to a version that supports C# 10. Unity 2020 generally supports up to C# 8, and you won’t be able to use C# 9 or C# 10 features without an upgrade.
Here are a few steps you can take:
-
Upgrade Unity: If you want to use C# 10 features, consider upgrading to at least Unity 2021 or later, as they have better support for newer C# versions.
-
Modify Your Code: If you want to stay with your current Unity version, you can modify your
structdefinitions:- Remove field initializers from the
structand instead initialize the fields within a constructor.
Here’s an example of how you might change your struct:
public struct EnemyStats { public int health; public int damage; // Explicitly declared constructor public EnemyStats(int health, int damage) { this.health = health; this.damage = damage; } } - Remove field initializers from the
-
Check Project Settings: Ensure your project’s settings are configured to use the desired C# version if you have upgraded Unity. You can typically find this setting under
Edit > Project Settings > Player > Other Settings > Configuration > Scripting Runtime Version.
By following these steps, you should be able to resolve the errors you’re experiencing with your struct and take advantage of the features you need.
December 8, 2025 at 3:49 pm #19107::I have Unity 2020, but I am able to use struct initializers. Does that mean I have C# 10? Is it because I have installed copies of Unity 2021 and 2022?
Give an example of struct initializers as well.
December 8, 2025 at 3:52 pm #19108::If you are using Unity 2020 and are able to use struct initializers, it suggests that your project settings may have been configured to use a newer version of C#. However, Unity 2020 generally supports up to C# 8, so it’s possible that you have installed a newer version of Unity (like Unity 2021 or 2022) and that your project is utilizing those settings.
Here’s an example of a struct with field initializers (also known as struct initializers):
public struct EnemyStats { public int health = 100; // Field initializer public int damage = 10; // Field initializer } // Usage EnemyStats enemy = new EnemyStats(); Debug.Log($"Enemy Health: {enemy.health}, Damage: {enemy.damage}");In this example,
healthanddamageare initialized directly in the struct definition. This is a feature that was introduced in C# 10, which allows you to give default values to fields in a struct without needing an explicit constructor.If you want to check which version of C# your project is using, you can also look at the
Project Settings -> Player -> Other Settingsin Unity, where you can specify the Scripting Runtime Version and the API Compatibility Level.If you find that you are using C# 10 features in Unity 2020, it’s likely that the project settings or the installed Unity version is influencing this behavior. If you want to ensure compatibility across versions, it’s best to stick to features available in C# 8 or lower.
has upvoted this post. -
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: