Need HELP - Changing a base class member in derived class declaration

Hi friends!
I have an enum member in my base class that I want to change from my derived class in the declaration section.
I manage to do so but unity gives me a warning that the “is assigned but its value is never used” although I do use it.
can anyone please help me understand what i’m doing wrong here? and how can I get rid of this warning?

I have a space ship that has modules, when the ship is initialized I check for the module type and instantiate the corresponding game object

CreateSlotsModules()

private void CreateSlotsModules()
{
    int j = 0;
    foreach (var slotData in ShipConfigData.Slots) // loop through slots in the ship config data
    {
                if (!slotData.Module) // if object is empty skip iteration
                {
                    j++;
                    continue;
                }
                SlotModule slot = slotData.Module.GetComponent<SlotModule>(); // get the slot module's base class
                switch (slot.Type) // <-----------  this is where I use the variable
                {
                    case Enums.SlotType.Weapon:
                        CreateWeaponModule(j, slotData);
                        break;
                    case Enums.SlotType.Shield:
                        CreateShieldModule(j, slotData);
                        break;
                    case Enums.SlotType.System:
                        break;
                    case Enums.SlotType.Armor:
                        CreateArmorModule(j, slotData);
                        break;
                }
                j++;
            }
}

Module Base Class

public class SlotModule : MonoBehaviour
    {
        public Enums.SlotType Type;
        public Collider SlotCollider;
        public ShipConfiguration ShipConfiguration;

        void Awake()
        {
            gameObject.layer = 16;
        }
        public virtual void WasHit()    { }
        public virtual void Repaired()  { }
    }

Module Example - derived class

public class ShieldModule : SlotModule
    {
        new readonly Enums.SlotType Type = Enums.SlotType.Shield; //  <------ this is where I assign the enum

        public float ModuleShieldPoints;
        public float RechargeDelay;
        public float RechargeRate;
        public float SPAfterRestart;
        public float RestartTime;

        public override void WasHit()
        {
            ShipConfiguration.UpdateShipShieldPoints(-ModuleShieldPoints);
            SlotCollider.enabled = false;
            Debug.Log("Shield Module down");
        }
        public override void Repaired()
        {
            ShipConfiguration.UpdateShipShieldPoints(ModuleShieldPoints);
        }
    }

The reason is most likely because the way you initiatise the enum. Is the error still there if you lose the “new” before the enum, because it is unneeded.

Thank you Alex
When I try initiating the enum without the “new” keyword it seems that it does not assign a value to the enum and it gets the default one, also VS show me a warning saying “ShieldModule.ModuleType hides inherited member SlotModule.ModuleType, use the new keyword if hiding was intended”
any Ideas?

Initialise the value in the constructor:

public class ShieldModule : SlotModule
    {
        public float ModuleShieldPoints;
        public float RechargeDelay;
        public float RechargeRate;
        public float SPAfterRestart;
        public float RestartTime;

        public ShieldModule() {
             this.Type = Enums.SlotType.Shield;
        }

        public override void WasHit()
        {
            ShipConfiguration.UpdateShipShieldPoints(-ModuleShieldPoints);
            SlotCollider.enabled = false;
            Debug.Log("Shield Module down");
        }
        public override void Repaired()
        {
            ShipConfiguration.UpdateShipShieldPoints(ModuleShieldPoints);
        }
    }

public Enums.SlotType Type; is a field, I recommend making it an abstract property instead. What you try to do right now is basically erasing the already existing field from the SlotModule class and introduce an entirely new field in the ShieldModule class.

public abstract class SlotModule : MonoBehaviour
    {
        public abstract SlotType Type { get; }
        public Collider SlotCollider;
        public ShipConfiguration ShipConfiguration;

        void Awake()
        {
            gameObject.layer = 16;
        }

        public abstract void WasHit();
        public abstract void Repaired();
    }

I highly recommend to use the abstract (C# Reference) keyword. Since WasHit() and Repaired() aren’t implemented in the base class SlotModule, there is no reason to make them virtual with an empty function body. Using abstract members is also a great way to check if you did override everything needed in derived classes.

Thanks!!
This works great!
And thank you for the detailed explanation, I’m new to programming, and still trying to get a my head around all the different keywords so this really helps to understand things better :slight_smile: