[Curious] Autocorrected to GetComponent<Rigidbody2D>().velocity

Rigidbody2D.velocity did not work for me, since it said velocity was not defined? Then I come back later and find that the code has been changed to GetComponent(). velocity. Can anyone tell me what happened? Or what GetComponent does?

Thank you!

RigidBody2D doesn’t work on unity 5. I had a few problems with it, but the following link helped me find a way around it. Hope this helps :slight_smile:


1 Like

GetComponent looks for a component with the corresponding name within the GameObject, as far as I know you can use it with any component such as rigidbodies, colliders, scripts, images etc.

Thnaks!! I also got an issue with RigidBody2D but your liknk works for me!!

The method shown in the videos is obsolete in newer versions of Unity. Instead, it wants to use

        this.GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);

Unity should offer to update this for you automatically when you save your script using the method Ben describes.


I think you can do this.transform and not getcomponent because Unity knows that every object has a transform component, but because rigidbody is something you add as you need it that is why they ask you to go and look for it.

I was having the same problem but one look at this solved it for me. Thanks a lot as well.

Privacy & Terms