Ben
Ben

Reputation: 16679

Hides inherited member warning

In the following Unity/C# code, the collider variable produces the following warning:

Warning CS0108 'Controller2D.collider' hides inherited member 'Component.collider'. Use the new keyword if hiding was intended.

What does this mean and how do I fix it?


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// require that our object has a box-collider-2d component
[RequireComponent(typeof(BoxCollider2D))]

// controller-2D script
public class Controller2D : MonoBehaviour {

    // stores a reference to our object's box-collider-2d component
    BoxCollider2D collider; // the warning occurs here

    // ...
}

Upvotes: 3

Views: 21885

Answers (2)

Siavash Mortazavi
Siavash Mortazavi

Reputation: 1862

It is a late answer, but you may find it useful. Before digging into your problem, here is an introduction:

  • In C#, you can define a method, property, indexer, or event as virtual (using "virtual" the keyword). By doing so, you are declaring that the virtual member can be overridden in a derived class.
    • Private members cannot be virtual, because they won't be inherited by the child classes, so they cannot be overridden.
  • If you don't declare a non-private member of a parent class with "virtual", but you add a member to a child class with the same name, it means you're hiding the parent member.
  • While "virtual" and "override" keywords don't apply to field members, the concept of hiding a field member and the "new" keyword do. This means if you define a non-private field member in a parent class, then define a field with the same name in the derived class, you're hiding the parent member. In your case the warning means MonoBehaviour (or one of its parent classes) has defined a public or protected field called "collider" of type "BoxCollider2D".
  • I would say in the cases that you're the author of the parent class, and you can see the consequences of hiding a parent member, you may want to do it (although not recommended).
  • But, in your case, where you don't own the "MonoBehaviour" source code, although adding the "new" keyword would suppress the warning message, but the whole situation should be avoided or you'll encounter weird polymorphism issues.
  • By avoiding, I mean simply rename your "collider" field to something else like "boxCollider2D"

p.s.1 By "weird polymorphism issues" I mean depending on your object pointer, the parent class member or the child class member will be used. You can read more here: https://stackoverflow.com/a/22809757/1854557 p.s.2 In my test code, when I add and remove the new keyboard, the result is the same. So it seems not adding the new keyword just generates the warning, but doesn't change the behavior of inheritance/polymorphism.

Upvotes: 8

JSON
JSON

Reputation: 1182

This is a warning that means a base class of Controller2D named Component already has a property with the same name collider. The warning is there to inform you that any implementation of Controller2D will use your definition of it and "hide" the base definition. To prevent the warning simply change the definition of collider to

new BoxCollider2D collider;

Then it will know that you mean to hide this priperty and the warning would go away.

There is not much reason to do it. Public and internal properties are already available. If the your property doesn't match or extend the the property you are hiding it will most likely cause issues, so if you don't know if you want to hide another property you should rename it.

Upvotes: 4

Related Questions