Hey guys, I’m currently starting out with Unity and got a question on a scripting topic.
I need to access a GameObjects Rigidbody component and got a line of code I don’t understand the syntax of.
Rigidbody rb = GetComponent();
I understand that the code above accesses the GameObjects Ridgidbody through the GetComponent function. I also understand that you need to store the value of the Rigidbody component inside of a variable, in this case rb. My question is: Why do I need to write Rigidbody before the variable?
Is it because of the type of the variable? Is Rigidbody in this case the type of variable? And is the type always the component I’m accessing? I just want to know the basic syntax of accessing components.
I hope you can help me understand this basic syntax. Thanks for helping me!
There is a new variable of type Rigidbody wich i will call it rb . And this new variable is equal to the Component Rigidbody attached to this gameobject.
So another example:
string SomeText = "Hello !"
There is a new variable of type string wich i will call it SomeText and its value is “Hello”
Here, first i declare 2 variables. One of type BoxCollider wich i will call it SomeCollider, and another of type GameObject wich i will call it OtherObject. This otherobject variable is set as public, so i will see it from Unity Inspector, and can be assigned by dragging it from the scene.
Then, i define the variable SomeCollider as the boxcollider of the object variable SomeCollider .
And then i change one of the SomeColldier property (isTrigger is a property of all colliders) to true.
Did you understood? I strongly recommend to spend yyour time watching tutpotrials to learn about all of this. You will needm, its the moooost basiuc thing in scripitng.!