Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Multiple items in an array entry

Discussion in 'Scripting' started by itsdawg43, Aug 30, 2023.

  1. itsdawg43

    itsdawg43

    Joined:
    Mar 7, 2023
    Posts:
    11
    I am trying to create a condensed list of resistance/weaknesses for my health manager for my game, and am having some trouble with getting it working. My current approach is to create an array of constructors containing a string variable and a float variable, but I can't quite get it to work. How it's supposed to work is if the damage type string from the attack matches any of the weaknesses, then it applies a multiplier to the damage, and matching resistances apply reductions. Here's what I have right now:

    Code (CSharp):
    1. public class HealthManager : MonoBehaviour
    2. {
    3.     [System.Serializable]
    4.     public class Weakness
    5.     {
    6.         public readonly string weakness;
    7.         public readonly float multiplier;
    8.  
    9.         public Weakness (string weakness, float multiplier)
    10.         {
    11.             this.weakness = weakness;
    12.             this.multiplier = multiplier;
    13.         }
    14.     }
    15.     [System.Serializable]
    16.     public class Resistance
    17.     {
    18.         public readonly string resistance;
    19.         public readonly float reduction;
    20.  
    21.         public Resistance(string resistance, float reduction)
    22.         {
    23.             this.resistance = resistance;
    24.             this.reduction = reduction;
    25.         }
    26.     }
    27.     public float health;
    28.     [SerializeField] private Weakness[] weaknesses;
    29.     [SerializeField] private Resistance[] resistances;
    30.  
    31.     public void TakeDamage(float damage, string damageType)
    32.     {
    33.         foreach (var weakness in weaknesses)
    34.         {
    35.             if(damageType == weakness.weakness)
    36.             {
    37.                 damage = damage * weakness.multiplier;
    38.             }
    39.         }
    40.         foreach ( var resistance in resistances)
    41.         {
    42.             if(damageType == resistance.resistance)
    43.             {
    44.                 damage = damage * resistance.reduction;
    45.             }
    46.         }
    47.         health = health - damage;
    48.     }
    49. }
    Here's how it's appearing in the editor:
    upload_2023-8-29_20-8-25.png

    And here's how I am trying to make it look:
    upload_2023-8-29_20-11-16.png

    If anyone has a tip or something to help out, let me know! Thank you.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Unity can't serialise
    readonly
    fields.
     
    itsdawg43 likes this.
  3. itsdawg43

    itsdawg43

    Joined:
    Mar 7, 2023
    Posts:
    11
    That would be the issue. It's always the tiny details :)