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
UnityEngine.MonoBehaviour:.ctor()
BaseCharacterClass:.ctor()
BaseMageClass:.ctor()
TestGUI:.ctor()
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.