• 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 GopalSir · Oct 23, 2020 at 03:52 PM · components

What exactly is a component ?

When I write a script in visual studio lets say "ForceAdder.cs" which just adds some force to the game objec to which it is attached and drop it on a GameObject , and I click "Play" .


What happens behind the scene ? Is there an object created of type "ForceAdder" and the corresponding gameobject has a list of all such components attached to it which are deriving from monobehaviour ?


So , somewhere behind the scene a code like "gameobjects.components[i] = new ForceAdder()" must get executed whenever a new component of type ForceAdder gets added to a gameobject , rigit ??


But i've seen we never write any constructors for our components , I never paid attention to it . Like everyone else , I was taught to use Start() for initialization . I know I'm missing a lot of things here can anyone help

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
4

Answer by Bunny83 · Oct 23, 2020 at 04:39 PM

Yes you have the right intuition. Though it's a bit more convoluted than this ^^. First of all Unity is a C++ engine. So all core elements of the engine are written in native C++ code. Mono / .NET is just the scripting layer. Every class derived from UnityEngine.Object has a corresponding representation on the native side. Most built-in components like Renderer, Collider, Transform, AudioSource, ... are completely implemented on the native side. The classes you use in scripting are pure managed wrapper classes.


Yes, Unity has a list of components for each gameobject. However this list is stored on the native side. All components which are derived from MonoBehaviour are essentially the same thing from the engine's perspective. So on the native side your "ForceAdder" component is just an instance of an internal C++ MonoBehaviour class. Of course the instance has a linked managed class (your actual .NET class). When you attach a component to a gameobject Unity will create an instance of it's internal MonoBehaviour class and also an instance of your managed class using the default constructor. This construction of your class can happen from anywhere in the native code. This is often done from a seperate loading thread in the background. That's why you shouldn't implement your own constructor because most of the managed Unity API is not thread safe and can not be used from outside the main thread.


For every MonoBehaviour derived managed class Unity stores some meta data on the native side. So when your code is compiled Unity analyses your class and searches for known callback methods and remembers those facts about your class. During the execution of a frame the Unity core just executes those special callbacks on your component instances when appropriate. I think here it's the best point to mention the Script Execution Order documentation.


Of course the managed land works quite different from native code, especially in regards to memory management. In C++ you have to explicitly destroy objects you don't need anymore in order to free the memory. In managed code this is not possible at all. Objects and memory management is completely under control of the garbage collector. So you can not explicitly destroy or remove the memory of a managed object. However we have the Destroy method which can destroy any UnityEngine.Object derived managed type. Though what this method actually does is destroying the native part of the object on the C++ side and mark the managed part as "dead". The managed objects are still there as long as they are referenced from somewhere. However Unity implemented [a custom == operator][2] which will "pretend" that a dead object is null / not existent.


A similar thing happens when you create a MonoBehaviour derived object manually with "new". The object is actually created on the managed side. However it doesn't have a native counterpart and is automatically considered "dead". Unity essentially doesn't even know that this instance exists. Since Components can only be attached to gameobjects and can't really exist without the hosting gameobject, Unity did not provide any way to create a component manually yourself. To create an instance of a gameobject you either have to drag and drop the script onto a gameobject in the editor so Untiy will properly create a component on the native side, creates the managed object for you and links everything properly, or you can use the AddComponent method of the gameobject class to do the same thing from code.

[2]: https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/

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
avatar image
0

Answer by arslanaliawan34 · 2 days ago

One line answer is : Component is called object of any class.

Comment
Add comment · Show 1 · 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 andrew-lukasik · 2 days ago 0
Share

One word comment is: No.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

140 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

Related Questions

How to add a component to all selected objects? 1 Answer

Import assets, linking their components automaticly. 0 Answers

Destroying Components from Editor Script Throwing MissingReferenceException 1 Answer

Array of usable MonoBehaviour's selected from Project View 0 Answers

Check if Object is of Type 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