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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by NanushTol, Dec 7, 2019.

  1. NanushTol

    NanushTol

    Joined:
    Jan 9, 2018
    Posts:
    121
    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

    Code (CSharp):
    1.  
    2. private void CreateSlotsModules()
    3. {
    4.     int j = 0;
    5.     foreach (var slotData in ShipConfigData.Slots) // loop through slots in the ship config data
    6.     {
    7.                 if (!slotData.Module) // if object is empty skip iteration
    8.                 {
    9.                     j++;
    10.                     continue;
    11.                 }
    12.                 SlotModule slot = slotData.Module.GetComponent<SlotModule>(); // get the slot module's base class
    13.                 switch (slot.Type) // <-----------  this is where I use the variable
    14.                 {
    15.                     case Enums.SlotType.Weapon:
    16.                         CreateWeaponModule(j, slotData);
    17.                         break;
    18.                     case Enums.SlotType.Shield:
    19.                         CreateShieldModule(j, slotData);
    20.                         break;
    21.                     case Enums.SlotType.System:
    22.                         break;
    23.                     case Enums.SlotType.Armor:
    24.                         CreateArmorModule(j, slotData);
    25.                         break;
    26.                 }
    27.                 j++;
    28.             }
    29. }
    30.  

    Code (CSharp):
    1.  
    2. public class SlotModule : MonoBehaviour
    3.     {
    4.         public Enums.SlotType Type;
    5.         public Collider SlotCollider;
    6.         public ShipConfiguration ShipConfiguration;
    7.  
    8.         void Awake()
    9.         {
    10.             gameObject.layer = 16;
    11.         }
    12.         public virtual void WasHit()    { }
    13.         public virtual void Repaired()  { }
    14.     }
    15.  

    Code (CSharp):
    1.  
    2. public class ShieldModule : SlotModule
    3.     {
    4.         new readonly Enums.SlotType Type = Enums.SlotType.Shield; //  <------ this is where I assign the enum
    5.  
    6.         public float ModuleShieldPoints;
    7.         public float RechargeDelay;
    8.         public float RechargeRate;
    9.         public float SPAfterRestart;
    10.         public float RestartTime;
    11.  
    12.         public override void WasHit()
    13.         {
    14.             ShipConfiguration.UpdateShipShieldPoints(-ModuleShieldPoints);
    15.             SlotCollider.enabled = false;
    16.             Debug.Log("Shield Module down");
    17.         }
    18.         public override void Repaired()
    19.         {
    20.             ShipConfiguration.UpdateShipShieldPoints(ModuleShieldPoints);
    21.         }
    22.     }
    23.  
     
  2. AlexN2805

    AlexN2805

    Joined:
    Nov 27, 2019
    Posts:
    33
    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.
     
  3. NanushTol

    NanushTol

    Joined:
    Jan 9, 2018
    Posts:
    121
    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?
     
  4. AlexN2805

    AlexN2805

    Joined:
    Nov 27, 2019
    Posts:
    33
    Initialise the value in the constructor:
    Code (CSharp):
    1. public class ShieldModule : SlotModule
    2.     {
    3.         public float ModuleShieldPoints;
    4.         public float RechargeDelay;
    5.         public float RechargeRate;
    6.         public float SPAfterRestart;
    7.         public float RestartTime;
    8.  
    9.         public ShieldModule() {
    10.              this.Type = Enums.SlotType.Shield;
    11.         }
    12.  
    13.         public override void WasHit()
    14.         {
    15.             ShipConfiguration.UpdateShipShieldPoints(-ModuleShieldPoints);
    16.             SlotCollider.enabled = false;
    17.             Debug.Log("Shield Module down");
    18.         }
    19.         public override void Repaired()
    20.         {
    21.             ShipConfiguration.UpdateShipShieldPoints(ModuleShieldPoints);
    22.         }
    23.     }
     
  5. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    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.

    Code (CSharp):
    1. public abstract class SlotModule : MonoBehaviour
    2.     {
    3.         public abstract SlotType Type { get; }
    4.         public Collider SlotCollider;
    5.         public ShipConfiguration ShipConfiguration;
    6.  
    7.         void Awake()
    8.         {
    9.             gameObject.layer = 16;
    10.         }
    11.  
    12.         public abstract void WasHit();
    13.         public abstract void Repaired();
    14.     }
    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.
     
  6. NanushTol

    NanushTol

    Joined:
    Jan 9, 2018
    Posts:
    121
    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 :)