Cannot explicitly hide variable member even using “new” keyword

So, here is my problem.

I have parent class called CameraBehaviour derived from MonoBehaviour

public class CameraBehaviour : MonoBehaviour
{
    public MinMax CameraViewRange;
 
    private Single CalculateViewRange() { ... }
}

and have two class ActionCamera2D and ActionCamera3D derived from CameraBehaviour

public class ActionCamera2D : CameraBehaviour
{
    [MinMaxRange (2f, 5f)]
    public new MinMax CameraViewRange = new MinMax(2f, 5f);
}

public class ActionCamera3D : CameraBehaviour
{
    [MinMaxRange(20f, 50f)]
    public new MinMax CameraViewRange = new MinMax(20f, 50f);
}

as you can see, both the script has CameraViewRange with new keyword and has different MinMaxRange attribute attached on each script, so the limit is will be vary between this script but still using the same CalculateViewRange calculation as the parent script.

But it turns out to be not possible, in Unity at least, raising an error:

The same field name is serialized multiple times in the class or its parent class.
This is not supported: Base(MonoBehaviour) CameraViewRange
  • MinMaxRange is a custom written attribute for property drawer.
  • MinMax is a custom struct that store min and max value.
  1. So, how can I overcome / deal with this problem ?
  2. Is my code pattern here is wrong ?
  3. Is there any workaround or better solutions for this ?
  4. Or is this the C# and Unity limitations ? ( but I’m not sure if it is )

thanks

Like the error says, it’s related to Unity serialization. You can either add [System.NonSerialized] tag or use properties, like so:

public abstract class CameraBehaviour : MonoBehaviour
{
    // abstract if not implemented in the base class, virtual if implemented
    public abstract MinMax CameraViewRange { get; set; }
}

public class ActionCamera2D : CameraBehaviour
{
    [SerializeField]    // public field is serialized by default, private only when told to
    private MinMax minMax;
    public override MinMax CameraViewRange
    {
        get { return minMax; }
        set { minMax = value; }
    }
}