# Is Rigidbody a variable type?

I been searching for this for a while online but found no good answer so now I'm asking here. I have a scripting question about Unity's Rigidbody.

When your making a game object move with Rigidbody you typically make a script for it that starts off with

public Rigidbody rigid;


Then you put in a GetComponent call like so

void Start() {
rigid = GetComponent<Rigidbody> ();
}


And the part I don't understand is how the Rigidbody variable works. Is it a variable type that unity is using cause even in the GetComponent it starts looking for Rigidbody not rigid. That seems like making a int variable then calling it saying Debug.Log(Int);

Also in unity script from the documentations it uses

function Start () {
var rb = GetComponent.<Rigidbody>();
}


Theres no Rigidbody in the declaration of the variable at all. Thats what made it even more confusing for me due to how GetComponent cleary shows it getting just the Rigidbody component but from the look of the script how does it connect to the game object?

With the C# example you drag and drop the GameObject in the Rigidbody variable even when the script is connected.

The reason why I'm asking is because I'm wanting to learn intuitively where I understand all the meaning of code like a second language. Is that normal?

Yes, Rigidbody is a type.

You have three different code snippets in your question, and they have three different explanations.

First, you have public Rigidbody rigid; This is a declaration of a variable. The variable's name is rigid, and the variable's type is Rigidbody. The type is what determines what that object can do, how it will behave. The name is simply what you call it when you refer to it.

Second, you have

void Start() {
rigid = GetComponent<Rigidbody>();
}


Here, you're calling the GetComponent() function to find a component, and assigning the result to the rigid variable (because that's what you named it, above). The <Rigidbody> bit is an indication that this function call is a C# concept called a generic method.

Generics are often used as way to write code when you don't know the precise types that would be involved. As a side-effect, they can also be used the way they are here in GetComponent, as a way to require you to specify a type parameter in a function call. This ensures that GetComponent both

• knows what kind of component to go search for and
• returns that component in a fashion that allows you to assign it to a variable of the desired type (Rigidbody) safely.

GetComponent actually looks like T GetComponent<T>(); the type of its return value is the same type you tell it about inside the angle brackets. That's why it can be correctly assigned to the variable rigid since they both are of the same type.

Finally, you've got:

function Start () {
var rb = GetComponent.<Rigidbody>();
}


This code isn't C#, it's Javascript. It is doing essentially the same thing as the second snippet, just with different syntax because it's a different language. It's possible you're confused because var is also a keyword in C#, where is serves as an indication that the type of the variable should be deduced by the compiler.

If you're unfamiliar with the C# programming language, you might find MSDN's interactive tutorial informative. It can help give you some experience with C# concepts outside of Unity, which may aid in your pursuits.

• I think I'm starting to get it but its still hard to understand. The basics of what I think I understand is that Rigidbody is the variable type for the definition but in GetComponent its saying Rigidbody as the actuall component. But GetComponent still confuses me as I seen it used with GameObject, Child, Component as in Component.GetComponent so I think the next part for my problem is understanding GetComponent completly. Do you have any references where to study it and its properties? Thanks alot for your quick answer too :D but did you mean T as in T for a rigidbody reference? – Spiritual Learning May 1 '18 at 2:27
• Rigidbody is always a type. In GetComponent<T>, T must be the name of a type; this tells GetComponent which type of component to go and find. Your variable, rigid, is not actually the component. It's a reference to one; you're just using it to store the result of GetComponent. I don't have any specific reference as I don't actually use Unity. I can only recommend reviewing the documentation. – user1430 May 1 '18 at 2:39
• @SpiritualLearning I can help a bit on that front. When you call GetComponent<T>() on its own, it's implicitly this.GetComponent<T>(), this being the component that's running this script. And that in turn is just a convenience method equivalent to this.gameObject.GetComponent<T>() - ie. "ask the GameObject I'm attached to for a component of this particular type T..." - so it's all the same method, just accessible in a few different scopes to save you some repetitive typing. – DMGregory May 1 '18 at 2:49