- Home /
What is the basic syntax of accessing components?
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! :)
Answer by tormentoarmagedoom · Mar 08, 2020 at 10:10 AM
Hello
You should look at some tutorial of basci scripting.
I do an example for you:
Rigidbody rb = gameObject.Getcompomponent<RigidBody>();
In this case this line of code means
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"
Now, lets complicate a little more:
BoxCollider SomeCollider;
public GameObject OtherObject;
SomeCollider = OtherObject.GetComponent<BoxCollider>();
SomeColldier.isTrigger = true;
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.!
Good luck!
There are so many ways to declare ,define and set variables, but this is the most "simple". As you learn more about Unity and scripitng will find some "variants" and structures.
Thanks for your answer. It definitely helped me. But what can be a type? Of course there are basic types like int, string and so on.
If I want to access the color of a Button Component and asign it to a variable, what type is it? Is it of type button? That's where I struggle.
I watched quite a lot tutorials and I thought that few of it covered that topic in depth. They didn't explain it good enough. If you know a good c# and unity tutorial let me know :) I am really interested in scripting and need a good tutorial to learn it.
the color is a property of the component button.
in the script, the color is of type Color. But not all variable types are inside UnityEngine library. At the top of the script, sometimes you will need to add new libraries. (where says using Unity.Engine)
I'd just like to add that often you don't actually need an explicit variable type in C#. This works perfectly fine for local variables (and it's just as fast and safe):
var rigidbody = GetComponent<Rigidbody>();To figure out the type of a property, method or variable, consult your IDE or the scripting reference. There are hundreds of types in Unity and many more in C# in general. You can't and don't need to know all of them just like you don't need to know every single property of every component. You just look up what you need and remember the stuff you use a lot.
Your answer
Follow this Question
Related Questions
C# accessing an array from a script in a different gameobject outside of the awake function 0 Answers
How can I access another script in my prefab to set a variable after my network player setup runs? 1 Answer
Script is setting variables but they're being reset 1 Answer