Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by zemmac · Jun 10, 2018 at 03:45 AM · componentsbehaviour

How are components created in Unity?

Hi, only a curiosity about how things works in Unity... When I add a component named "airplane", for example, and insert two scripts ("Move1" and "Move2", for example) and a Rigidbody2D, I have at least three classes... The object created when I start the game is an instance (named "airplane") of a class (Airplane) that inherit all this three classes (Move1, Move2 and Rigidbody2D)? Or it is a intance "airplane" that has three object of the classes Move1, Move2 and Rigidbody2D? I don`t think so, since "this" inside Move1 or Move2 seems to return things of Airplane. By the other way, when I use getComponent I can get instances of Move1, Move2 or Rigidbody2D already created... wow... So they are components inside my class Airplane? I didn`t get this yet. Someone can help? Thanks.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Bunny83 · Jun 10, 2018 at 08:06 AM

You seem to be quite confused here ^^.


Component derived classes (keep in mind MonoBehaviour is also derived from Component) are seperate objects which are just "bound" to a gameobject. A gameobject is basically just a container object for components. Each component is a seperate object instance. Each component is meant to add additional behaviour or properties to your gameobject. Not to the "gameobject class" but to the composition of objects. The gameobject class is basically just a mediator between all components of a logical object.


So if you attach your Airplane, Move1, Move2 and a Rigidbody2D component to a gameobject you will have 6 seperate objects:

  • the GameObject which acts as container.

  • the Transform component which gives the gameobject it's position, rotation and scale and the linkage to the scene / worldspace. Every gameobject always has a Transform component

  • the Rigidbody2D component which basically handles the interaction with the physics system

  • Your custom components Move1, Move2 and Airplane


Each of those components are from a pure OOP point of view seperate objects. However every component has a "link" (a property) to the container gameobject it belongs to. The "this" reference from within any class always only corresponds to that class instance. So "this" insice Move1 will only have access to things inside the Move1 class. To access other components on the same gameobject you have to use GetComponent.


If you're still confused you may want to share parts of your scripts so we address your problems on concrete examples.

Comment
Add comment · Show 2 · 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 zemmac · Jun 10, 2018 at 07:40 PM 0
Share

It make sense. Thank you... I’m just confused about one thing... this.name inside a method in ans instance of $$anonymous$$ove1 (script inside game object airplane) returns to me the name “airplace” when I would expect it returns the name of the instance for the script component $$anonymous$$ove1 (example: move1... don’t know how unitt name instance of classes that we create inside game object)... maybe I didn’t see something obvious. Thanks again. I really do not jave a code... it is only a theorical doubt

avatar image Bunny83 zemmac · Jun 10, 2018 at 08:14 PM 0
Share

All components have a name property but it's basically just an shortcut for gameObject.name. $$anonymous$$eep in $$anonymous$$d that name is not a field but a property, so the implementation in a derived class can be different. Though it's not a virtual property on the C# side. The different behaviour happens in the C++ core of the engine.

avatar image
0

Answer by zemmac · Jun 12, 2018 at 03:29 AM

Now it is clear... I thought name property came from C# side. Thanks.

Comment
Add comment · 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

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

83 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

Related Questions

Why are some components not inherited from Behaviour when they should? 1 Answer

Multiple Game Objects, different components 1 Answer

Documenting Unity classes/ components/ system 1 Answer

Infinite Loop while Removing Components 1 Answer

Cannot enabled / disable a script component? 2 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