Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by amkay · Mar 08, 2020 at 09:11 AM · scripting problemgetcomponentsyntaxaccessing

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! :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tormentoarmagedoom · Mar 08, 2020 at 10:12 AM 0
Share

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.

avatar image amkay · Mar 08, 2020 at 12:50 PM 0
Share

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.

avatar image tormentoarmagedoom amkay · Mar 08, 2020 at 08:03 PM 0
Share

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)

avatar image BastianUrbach amkay · Mar 09, 2020 at 03:25 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

223 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# accessing an array from a script in a different gameobject outside of the awake function 0 Answers

script with individual function for each gameObject or public script variable to solve inventory system problem 1 Answer

When or where do I set a prefab script subclass property? 2 Answers

checking Bool true from another Script? 2 Answers

Difficulties accessing other components 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges