Warning cs0108 (doubt)

So, I was reading an article from Unity itself:

Because I was getting the warning:

warning cs0108: ‘Player.rigidbody’ hides inherited member ‘Component.rigidbody’. Use the new keyword if hiding was intended

Because I had the following code:

    private Rigidbody2D rigidbody;

    private Animator animator;

    void Start()
    {
        rigidbody = GetComponent<Rigidbody2D>();
        animator  = GetComponent<Animator>();

    }

So I changed how the article says and where Unity was accusing:

    private new Rigidbody2D rigidbody;
    private Animator animator;

    void Start()
    {
        rigidbody = GetComponent<Rigidbody2D>();
        animator  = GetComponent<Animator>();

    }

Since the article says “The CS0108 warning is displayed when a variable was declared with the same name as a variable in a base class, but does not use the new keyword.”.
But see an interesting thing happened, because Rigidbody2D needs the NEW operator but Animator doesn’t?

Again sorry for my english. Thanks!

Because Component has a (deprecated) “rigidbody” property, but not one for “animator”, (though it does have an “animation” property)

1 Like

Thank you! I’m starting with Unity, I don’t know much about the library yet, I’m a little bit in the dark too because my VScode doesn’t seem to want to pull the library properly. So I’m looking for readings and help in the forums.