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)
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
namespace UnityEngine
{
public partial class Component
{
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[Obsolete("Property rigidbody has been deprecated. Use GetComponent<Rigidbody>() instead. (UnityUpgradable)", true)]
public Component rigidbody
{
get { throw new NotSupportedException("rigidbody property has been deprecated"); }
}
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[Obsolete("Property rigidbody2D has been deprecated. Use GetComponent<Rigidbody2D>() instead. (UnityUpgradable)", true)]
public Component rigidbody2D
{
get { throw new NotSupportedException("rigidbody2D property has been deprecated"); }
}
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.