The Rigidbody2D Changes: Unity 4.6 vs 5.2.2


It may be common knowledge, but it wasn’t for me. Actually, ‘it’ drove me up the wall. The ‘it’? Rigidbody2D in Unity; specifically the changes in the call between Unity 4.6 and the new 5.2.2. This snippet is for new Unity Devs like myself who’s scoured the internet for reasons why the following lines such as:

Rigidbody2D.velocity.y = speed *-1;  

or

rigidbody2D.Addforce (Vector2.up * XXf);

throw up null errors.

Simple Answer:

They no longer work.

Yes, the Unity team changed them with Unity 5. I’m new to the party, so I didn’t drink the 4.6 Rigidbody2d Kool-Aid that long, so thank goodness I don’t have lines and lines of code to change.

 

Simple Work Around:

Learn and Love GetComponent<>. It loves you. It needs you. It has cookies and cocoa. It doesn’t want you to tear out your hair or firebomb your neighbor’s place. It wants you to come to the Unity fold and be swanky with all the other new calls. You can see a prime example in Quill18’s Flappybird tutorial.

ex:

rigidbody2D.Addforce (Vector2.up * XXf);

should be written as below:

GetComponent<Rigidbody2D>().Addforce(Vector2.up * 10f);

 

***

I’m not above asking for tips!

Bitcoin: 1zcqAZaq99XYnWdDCWsYEVX4rwb5ikyvz

Doge: DKERrZi1VLbaJovo6xFWz76dQq3826DP93