Reputation: 16679
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
Reputation: 1862
It is a late answer, but you may find it useful. Before digging into your problem, here is an introduction:
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
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