Jump to content
  • Advertisement
CompuServe

GetComponent in Unity

Recommended Posts

Posted (edited)

Look, I know programming. Getting into Unity now. Started following a tutorial on scripting but got stuck mentally on the first step.
He's trying to explain how GetComponent works but does a really poor job.
So, this is the code:

public Rigidbody rigid;

void Start () {

rigid = GetComponent<Rigidbody>();

}

Now what I don't understand is if rigid is already declared as the type Rigidbody why does it have to be assigned via GetComponent?
If someone could explain this I would be sooo happy.
Thanks,

Compu

Edited by CompuServe

Share this post


Link to post
Share on other sites
Advertisement

See: https://docs.unity3d.com/Manual/ControllingGameObjectsComponents.html

This is how you access those components.

rigid = GetComponent<Rigidbody>();

then for example in FixedUpdate() you would use rigid to preform your motion, ect....

Same applies to having something like myAnimator = GetComponent<Animator>(); Then you can set the animation speed like so myAnimator.speed = 3f under Start() {}

Or in FixedUpdate() {} you can get the animation frame: myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Default") to do certain things, ect...

GetComponent<Rigidbody>(); essentially allows you to access the rigidbody component attached to the object. If you have a script for Player Movement attached to a Player Object, then you'll want to be able to access the rigidbody component to make movements under FixedUpdate() as needed. This allows you to do that.

Share this post


Link to post
Share on other sites
Quote

Now what I don't understand is if rigid is already declared as the type Rigidbody why does it have to be assigned via GetComponent?


So, if you declare a variable of type Rigidbody, which is class

public Rigidbody rigid;

You have a null object, since class needs to be initialized. To make an instance of a class you'd normally use a new keyword or if the instance already exists, you want to get it, this.GetComponent<T>() lets you get an existing instance of type T from GameObject that has this script attached to. There is also another way to get the instance - since this field is public, it's also serialized, so you'll see an empty field in inspector where you can drag there which rigidody you want to access.

Unity won't automatically inject instance to this field since it would be less flexible and would make too much magic.

By the way, if it doesn't need to be public, don't make it public. If you want to see fields in inspector, you can use [SerializeField] attribute. Remember about encapsulation.

Share this post


Link to post
Share on other sites

When you type this:

public Rigidbody rigid;

You're declaring a variable of type 'Rigidbody'.  Every instance of the script will now have this variable.

You are not actually assigning a value to it; it is 'null' until you do so.

GetComponent is a method that every script inherits naturally (it comes from the MonoBehaviour class we inherit to declare a script).  That method searches the GameObject the script is attached to, looks for a component of the given type (in this case, Rigidbody).  If it can find a component of that type, it returns it.  If it can't, it returns 'null' instead.

So that code is basically saying "on Start, look for a Rigidbody component and assign it to my variable `rigid`".

Like Prastiwar said, once you declare that variable, you can cut the entire Start method out and just drag and drop the Rigidbody component from the Inspector, which sets up the reference right off the bat – no extra coding necessary.  This is a more standard and clean way of doing things.

But probably the tutorial you're reading is going to get to that part later, so maybe don't worry about it so much right now.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!