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. Dismiss Notice

C" Instance question

Discussion in 'Scripting' started by jieep13, Jan 25, 2016.

  1. jieep13

    jieep13

    Joined:
    Jan 25, 2016
    Posts:
    20
    Hello folks,

    How have you been?

    I have a simple question, but I really don't know how to do this task. I just want to make an instance of one class in another class, but always that I try to sign in, the Unity console, shows me an alert:

    * I try to make the instance in this way:
    GameController Gc = new GameController () - This is just an example.

    *And the console shows me the alert below, and the instance is always null:
    "You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all"

    I really don't know how to do this instance because I've learned to do the instance in this way. Could anyone help me in this issue?

    Thanks a lot!

    ** Sorry by my question, I'm still a noob in programming. =D
     
  2. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    The error message tells you exactly what you're doing wrong - you should use the AddComponent() method :)

    Example:
    Code (CSharp):
    1. GameController gc = gameObject.AddComponent<GameController>();
    where gameObject is the object you want to add this component to.
     
  3. jieep13

    jieep13

    Joined:
    Jan 25, 2016
    Posts:
    20
    Thanks for the answer. But ifI add this component, wont I have a duplicate error?
     
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,032
    Why do you think you would have that?

    EDIT: Thinking of a different thread. Need food.
     
    Last edited: Jan 25, 2016
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    to make a completely new GameObject with your Component attached, use...

    Code (CSharp):
    1.  
    2. void Start()
    3.     {
    4.         var go = new GameObject("myGameObject", typeof(GameController));
    5.     }
     
  6. cjdev

    cjdev

    Joined:
    Oct 30, 2012
    Posts:
    42
    The reason for instantiating the Component this way is Unity's use of a Component-based design. Basically, instead of creating separate, individual objects that are loosely related (like in most C# programming), the idea is to create a single object (the GameObject) that acts as a container for other objects (Components), each of which are made to fulfill a single, discrete role. This pattern is great for game development because we can re-use the components between objects and still support any number of objects with varying traits. What it means in practice is that instead of using the new keyword to make our Components (any script that inherits from MonoBehaviour) we use AddComponent<T>() on the GameObject we want to contain it. When we do this the GameObject makes a unique instance of the Component, there can only ever be one, and then we can get a reference to it with GetComponent<T>().
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    That's not true, you can have multiple instances of a component on the same game object (unless the component follows the singleton pattern I guess)

    There are however reasons that composition can be desirable over inheritance, and plenty of discussions explaining why in far more articulate ways than I can manage. Google search composition over inheritance for some examples.
     
  8. cjdev

    cjdev

    Joined:
    Oct 30, 2012
    Posts:
    42
    You're right, my mistake. I was thinking of other types of Components, but you can have multiple instances of the same type of Script Component.

    As for composition vs inheritance, I can't say I've ever really understood that debate in the context of Unity. To me a script is a Component and they are a part of a GameObject.I know it's not really technically true that the Component is a part of a GameObject but with the way Unity integrates them with Components it might as well be. In contexts that don't involve MonoBehaviour I can certainly understand where the debate stems from and what the general pros and cons are but otherwise I see the lines blurring in Unity's case.
     
  9. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    These answers are all correct, but if you really want to use the "new" keyword(which I don't recommend), then your class cannot inheriting from the MonoBehaviour class.