GetComponent in Unity

Started by
5 comments, last by Prastiwar 4 years, 8 months ago

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

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.

Programmer and 3D Artist

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.

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.

[twitter]Casey_Hardman[/twitter]

On 3/8/2019 at 9:20 AM, Prastiwar said:


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.

errm...I'm having trouble understanding this fully. Ok, so it's a null object to begin with...when I assign rigid via this ->


rigid = GetComponent<Rigidbody>();

do I instantiate something? (or is it initialize??? I'm having a headache right now trying to differentiate between these expressions please guys...help me out) and...well yeah...as u noted: can I get it

it's just that...you express it like this: "you'd normally use" errm...what does this mean? are you suggesting that the above is an optional form of doing it?

When you assign rigid by


rigid = GetComponent<Rigidbody>();

You get existing instance, the instance of Rigidbody was created by Unity in Editor when you added the Rigidbody in the inspector.

3 hours ago, CompuServe said:

it's just that...you express it like this: "you'd normally use" errm...what does this mean? are you suggesting that the above is an optional form of doing it?

I meant, pure classes would be needed to initialize this way


public class SomeClass
{
    public int SomeInteger { get; set; }
}
...
SomeClass mySomeClassVariable; // value is null
mySomeClassVariable = new SomeClass(); // value is new created object of type SomeClass

You don't make this way with Unity's components (Rigidbody, MonoBehaviour) because Unity prefers you to use Unity Editor (inspector), you can initialize it via `AddComponent<Rigidbody>()` tho, it will use make an instance underhood.

This topic is closed to new replies.

Advertisement