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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #19105
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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?

    #19106
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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:

    1. 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.

    2. Modify Your Code: If you want to stay with your current Unity version, you can modify your struct definitions:

      • Remove field initializers from the struct and 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;
          }
      }
    3. 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.

    #19107
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

    #19108
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    1
    ::

    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, health and damage are 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 Settings in 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.
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: