changing this.rigidbody2D.velocity to this.GetComponent().velocity
Hi,
I am learning Unity3D. First of all I ain't good at C#. But, one thing I understand is GetComponent<>() is generic method. Also GetComponent<>() ain't a static method otherwise it wouldn't have been used by instance of class(object) i.e. this.... I know C++ && Java so I figured out that C# is like Java has generics and not templates.
But, I don't understand the reason behind changing from
this.rigidbody2D.velocity to this.GetComponent().velocity
why did they change it to... generic method? I just trying to understand the details. That's it
Answer by SarperS · Jun 22, 2016 at 09:12 AM
Over time, we’ve been inconsistent in wether or not we added these quick accessors to the API for new components. For some we did, for others we didn’t, and by now it’s not clear why some components have them and others not. These accesors are also not nice for modularisation. When you modularise, you want to make sure that all the dependencies “point in one direction”. It’s okay for the physics module to talk to things in the core module, but it’s not okay for the core module to talk to the physics module directly, because it might not be there. Turns out that is exactly what we have here. We have the MonoBehaviour class, which is something that we want to put in a “core” module. but it has a property getter implementation for .rigidBody that returns a RigidBody object, which should obviously live in the physics module, that the core module shouldn’t be able to reference. For Unity5, we decided to bite the bullet, and remove all the quick component property getters with the exception of .transform (since that one belongs in the core module, there’s no need for it to go, and it’s also by far the most used one) [1][2]
Source: http://blogs.unity3d.com/2014/06/23/unity5-api-changes-automatic-script-updating/
Your answer
![](https://koobas.hobune.stream/wayback/20220612083430im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is a gameobject moves at start? 0 Answers
how to fix velocity for jump and movement or fall in 2d pltform game with gravity 4 Answers
how to clamp( set max value) the velocity of a 2d rigidbody? 1 Answer
How do I make my character move on the Z axis on 2d rigidbody? 0 Answers
How to have the same speed on Rigidbody2D in all resolutoins ?? 0 Answers