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 dont 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.
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.
It make sense. Thank you… I’m just confused about one thing… this.name inside a method in ans instance of Move1 (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 Move1 (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
Now it is clear… I thought name property came from C# side. Thanks.