[Solved] Component.rigidbody2D is deprecated

In trying to follow the Ruby’s Adventure tutorials, I find that Component.rigidbody2D is deprecated and I should “use the new keyword”. What’s the new keyword? What is meant by “hiding” and why was the old code deprecated? Thanks for your help.

Assets\Scripts\RubyController.cs(9,17): warning CS0108: 'RubyController.rigidbody2D' hides inherited member 'Component.rigidbody2D'. Use the new keyword if hiding was intended.

Rigidbody2D rigidbody2D;

    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>;
    }

Just rename “rigidbody2D” to something else is all. It is never a good idea to name your variables the same as something Unity is already using. Ignore the recommendation to use the “new” keyword here, as that is incorrect advice when you’re dealing with components in Unity.

Rigidbody2D myRigidbody2D;

    void Start()
    {
        myRigidbody2D = GetComponent<Rigidbody2D>();
    }

No more warning ^^^

It means that you should not use rigidbody2D because it already exists as a type in Unity C#. Use another word, rb2D, for example. Your line of code is wrong too, you forgot to add () at the end of it.

Actually, to be pedantic, rigidbody2D is not the type, it was the name of a MonoBeviour property which returns a reference of the RigidBody2D component attached to that object. It is now deprecated, though, along with several other component easy-access properties that Unity decided was not a good idea to have around.

Either way, though, it’s still the same problem- the name rigidBody2D is already taken for something else.

Thanks again. What does “cannot implicitly convert” mean?

You haven’t mentioned anything about that yet, but you usually get that error in an assignment statement when you are trying to set one variable to the value of another variable, but the two variables are different types.

a lot of times this is due to a mistake, like if you accidentally write:
Vector3 playerPos =Player.transform;
when you meant to write
Vector3 playerPos = Player.transform.position;
In the first statement I’m accidentally trying to store the reference to a transform in a Vector3 variable. The error message is saying “You can’t just automatically convert a Transform to a Vector3”.

Sometimes you get that error message when you can convert the value, but you have to tell the compiler explicitly, for example:
int x = player.transform.position.x;
Will give you that error, but you could write-
int x = (int)player.transform.position.x;
So in that case, the code will not implicitly convert a float to an int, but if you explicitly tell it to than it will.

I have it now- trying to fix the error, I let Visual Studio change ‘GetComponent’ to ‘GetComponents’.

this is what implicit conversion means

Vector2 myVec2 = new Vector2(8, 5);
Vector3 myVec3 = myVec2;

these types have a defined implicit conversion

an explicit conversion would be to manually cast one type to another

Vector2 myVec2 = new Vector2(8, 5);
Vector3 myVec3 = (Vector3)myVec2;

this is valid because two types are compatible (and, in this case, already implicitly-convertible)

Not according to the documentation: https://docs.unity3d.com/ScriptReference/Rigidbody2D.html Why would they deprecate it? What would they replace it by?

They didn’t deprecate the class, they deprecated the shorthand property. Notice the small g in the name.
This kind of shortcut was neat in the beginning, however, it started to saturate the GameObject base class once they introduced many of them. It was probably a maintenance hell and a clutter to work with.

Here’s a full list of them, all obsolete, meaning not to be used

public Component rigidbody { get; }
public Component rigidbody2D { get; }
public Component camera { get; }
public Component light { get; }
public Component animation { get; }
public Component constantForce { get; }
public Component renderer { get; }
public Component audio { get; }
public Component guiText { get; }
public Component networkView { get; }
public Component guiElement { get; }
public Component guiTexture { get; }
public Component collider { get; }
public Component collider2D { get; }
public Component hingeJoint { get; }
public Component particleSystem { get; }

It was done like this probably because the early GetComponent wasn’t a generic and had to unbox its values, making it rather slow when used in Update. By providing system access (a direct reference), in the form of property shortcuts, the code would run much faster, but it was a PITA that had to go away.

My whole point was that the error message was not about Rigidbody2D. It’s about rigidbody2D, which is not the same thing but the names are confusingly similar.