3

Is there a way to instantiate a class that derives from MonoBehaviour such as the example bellow without getting the warning: "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"

Example:

public class e1506131012test2 : MonoBehaviour 
{
    Move move = new Move();
    //Move move = gameObject.GetComponent<Move>();

    void Update()
    {
        move.Printing();
    }

}

public class Move : MonoBehaviour 
{
    public int number = 5;

    public void Printing()
    {
        print(number);
    }
}
cubecube
  • 107
  • 1
  • 3
  • 8
  • 1
    Could you please clarify what part of "This is not allowed" needs more explanation? Alternatively you can state your actual goal and there could be different approaches to reach it. – Alexei Levenkov Jun 15 '15 at 16:06
  • Try : AddComponent(Move); in "class e1506131012test2" and don't let Move derive from MonoBehaviour... – Johan Jun 15 '15 at 16:21
  • AddComponent(Move); or gameObject.Addcomponent(); give me another console error (see the answer bellow). I wish to instantiate a class that derives from MonoBehaviour (eg: class myClass : MonoBehaviour into myOtherClass) – cubecube Jun 15 '15 at 16:53

2 Answers2

0

There are a few ways you can do this, the easiest is probably to use AddComponent as the error message suggests:

Move move;
void Start()
{
    move = gameObject.Addcomponent<Move>();
}

The reason you can't just new up an object that derives from MonoBehaviour is that such objects must be a component of a GameObject. As such, whenever you create one you have to ensure that it is added in a valid way.

Adam H
  • 1,523
  • 11
  • 21
  • Thank you, when I use Move move = gameObject.Addcomponent(); I get an error in the console saying: "A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.gameObject" Hopefully there is a way to instantiate a class derived from MonoBehaviour without getting console errors or warnings. Cheers – cubecube Jun 15 '15 at 16:48
  • Yeah sorry I wasn't thinking, I've updated my answer to something that should work. Incidentally, if your `e1506131012test2` GameObject is always going to have a `Move` component, you may want to add it via the inspector. – Adam H Jun 15 '15 at 16:56
  • Thank you Adam, so that means I have to create a new gameObject each time I wish to instantiate a class derived from MonoBehaviour and attach the class scrip to it, right? I wish to keep it on the code level (no drag and drop in the Inspector), would you have a suggestion on what would be the best way to write that? Cheers – cubecube Jun 15 '15 at 17:05
  • You can't have a MonoBehaviour (component) without a GameObject, but you can have multiple MonoBehaviours (components) attached to a single GameObject. Don't be confused by the use of `gameObject` though, that's a reference to the GameObject that the component is currently a component of. – Adam H Jun 15 '15 at 17:13
  • Thanks Adam for your help – cubecube Jun 16 '15 at 02:20
0

Just don't derive it from MonoBehaviour.

public class Move 
{
    public int number = 5;

    public void Printing()
    {
        print(number);
    }
}

And if it really must be a MonoBehaviour, that means you probably have it on a prefab, in which case you use Instantiate().

maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37