Jason Finney (demonslayer112)

1 answer · asked @ video mark 2:24 · Lesson: Accessing Components · Course: C# Bootcamp for Unity

Why do you put rigidbody in the script?

At 2:20 you make the public Rigidbody and set it to rigid. I been attempting to learn intuitively and dont understand, you make a variable which seems to have a type of Rigidbody and name it rigid but with getcomponent you look for Rigidbody which looks like a function. Why look for rigidbody type variable when you named it rigid, or does it not even need to have that public Rigidbody rigid line?

  • crew

    The name of the variable doesn't affect how it's accessed. I could name it rb and it would be used in the same way. GetComponent is used to access different components like a Rigidbody. Unity will automatically do this in the background for certain components like the Transform component. 

    Unity has gone back and forth on doing this with Rigidbodies, even within their own documentation they reference it in a same way. It looks like at the moment with Unity 2018 they automatically cache the Rigidbody as well so it's not necessary. Note this will only be true if you manually assign it.

    Also just to clarify your last question, yes declaring the variable "rigid" of type "Rigidbody" is required. This lets us determine specifically which Rigidbody we want to affect. If we had 5 Rigidbody objects in our scene and wanted to affect one specifically we'd need to create a variable to allow us to affect that one object.