Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How components are created in C#

Discussion in 'Getting Started' started by zemmac, Jun 10, 2018.

  1. zemmac

    zemmac

    Joined:
    Jun 10, 2018
    Posts:
    5
    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.
     
    Last edited: Jun 10, 2018
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    A class that inherits from MonoBehaviour is a component, and most scripts in Unity have a class that inherits from MonoBehaviour. Thus, the situation you describe probably means 4 components attached to one object: Airplane, Move1, Move2, and Rigidbody2D. Probably; your use of "insert two scripts" is a little vague, so I don't know what "inserts" means to you.

    Also, bear in mind that a GameObject is basically just a list of components attached to it. That might help clarify things for you, that everything is simply attached to one game object, but they are all still separate classes.
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,091
    GameObjects are containers that hold Components. GameObjects can't hold other GameObjects and Components can't have their own Components. Anything you can add to a GameObject has to have inherited from Component at some point (eg MonoBehaviour inherits from Behaviour and Behaviour inherits from Component).

    https://docs.unity3d.com/ScriptReference/GameObject.html
    https://docs.unity3d.com/ScriptReference/Component.html

    Components listed in the Inspector are showing the order that they were added to the GameObject.

    https://docs.unity3d.com/Manual/UsingTheInspector.html

    Components do have a method called GetComponent, but that's just a shortcut for accessing the GetComponent method of the GameObject. Every Component has a property (linked below) for accessing the GameObject it is attached to and when you use GetComponent it's going through that property.

    https://docs.unity3d.com/ScriptReference/Component-gameObject.html
     
    Last edited: Jun 12, 2018
    jhocking likes this.
  4. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,500
    Hi @zemmac Wellcome to this forum.
    I was looking that you have done the same question here:

    https://forum.unity.com/threads/how-are-components-created-in-c.535412/#post-3527168

    Making a component is easy, did you take a look at this video tutorials?
    https://unity3d.com/learn/tutorials/s/scripting
    https://unity3d.com/learn/tutorials/topics/scripting/scripting-primer-and-qa?playlist=17117

    You do not need to use inheritance at the beginning. You can just use a class that derives from mono like the default c# script and use getComponent to access another game object variables.
    You can create a game object name it "Manager" and in it components that "Moves" 1 & 2 the aeroplanes. Follow first the tutorials
     
    Last edited: Jun 12, 2018