Create a "new" Monobehavior not allowed

Hi, I am currently doing a tutorial and stumbled upon this 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

I have checked google and they all say to created a new gameObject, which I totally not understand. The code is as follows.

public class TestGUI : MonoBehaviour {

private BaseCharacterClass class1 = new BaseMageClass();

I understand this is not allowed, but the person who gave the tutorial does not receive this warning in Unity, unless its Unity 5 only, he uses 4. Someone already complained about this warning never got a reply. How would you fix it. The code works, but I don’t like the warning to come up every time I press play. The tutorial is found here… link text

I have tried creating game object, get different error

BaseCharacterClass class1 = gameObject.AddComponent("BaseMageClass");//UnityEngine.GameObject.addcomponent(string) is obsolete.

Also removed Monobehavior as someone suggested, which I thought would not work, and I guessed right, it didn’t work.

to create a new GameObject use Instantiate() as GameObject;
If you truly want to create a new instance of a class, make it so it no longer extends a MonoBehavior.

You can do this inside of a method.

GameObject myObject = new GameObject();
BaseCharacterClass class = myObject.AddComponent<BaseMageClass>();

I actually just had to deal with this in my code. If you want to have access to a Monobehaviour you have to get it by either using:

MyClass a = myGameObject.GetComponent<MyClass>();


MyClass a = myGameObject.addComponent<MyClass>();

Sorry I’m terrible at JS and don’t want to tell you wrong, so I had to put it in c#