I am Little Confused

i have a question so when we write

Rigidbody rb

are we using rigidbody as a data type if yes then cant we just assign a variable then assign get component that ways i am not getting the logic here

can someone explain


Hi Blaze,

Welcome back to our community! :slight_smile:

Rigidbody rb; is a declaration (and definition) of a variable. In C#, all variables require a name and a data type. Here, the type is Rigidbody. Then the variable is able to hold a reference to an object of type Rigidbody. Otherwise, its default value is null (= no object reference).

In line 22, your code assigns a reference to the rb variable, given GetComponent returns an object.

Did this clear it up for you? :slight_smile:

See also:

Yes i get but i have one more question
This Is also a valid statement for C#

Void start
rigidbody rb = GetComponent();
so can we declare and reference at the same time in start

Yes, that’s right. In your example, the rb variable gets declared within Start, and an object reference gets assigned in the same live if GetComponent returns a reference.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms