- Home /
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 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.
Answer by Kiwasi · May 01, 2015 at 07:53 PM
You can do this inside of a method.
GameObject myObject = new GameObject();
BaseCharacterClass class = myObject.AddComponent<BaseMageClass>();
Thanks elder, this is the closes one I got that works, but it also creates an empty gameObject in the inspector which I don't want, had to split the code so only one game objects is created and not every frame. I just need to get a variable like a string or an integer, from a different script. Unless I use new game object then destroy game object. This is how I did it with my other scripts.
Anyways you gave me an idea, ins$$anonymous$$d of creating a new gameObject and adding the script, I added the script directly to this gameObject. I have the code like this and it works.
public class TestGUI : $$anonymous$$onoBehavior {
BaseCharacterClass class1;
}
void Start () {
class1 = gameObject.AddComponent<Base$$anonymous$$ageClass>();
}
void OnGUI() {
GUILayout.Label(class1.CharacterClassName);
}}
Answer by Sessional · May 01, 2015 at 04:55 PM
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.
Is it something like this?
BaseCharacterClass class1 = Instantiate() as GameObject.AddComponent("Base$$anonymous$$ageClass");
which does not work, how would I write this code so it would not complain inside Unity? Im kinda new at this approach which I'm trying to learn.
If you don't understand prefabs you will need to look up prefabs first, but it goes like this: GameObject go = Instantiate(gameObjectPrefab) as GameObject; BaseCharacterClass class1 = go.GetComponent();
This is not a prefab, this is a class from a different script. For me a prefab is where you create a new prefab in the inspector and drag a set gameObject on it.
first script is called BaseCharacterClass.cs this script is called TestGUI. Im trying to get data from the first script for example.
void OnGUI()}
GUILayout.Label(class1.CharacterClassName);
Im trying to learn a new approach otherwise I'll go back to $$anonymous$$e or find a different approach, but it would be nice to learn something new.
If all you want to do is add the Component it's as simple as making the Base$$anonymous$$ageClass extend $$anonymous$$onoBehavior and then adding it as a component to the game object. If it is on the game object you can access it with GetComponent().CharacterClassName;
The problem comes from the fact that Unity will not let you use a constructor on an object that extends $$anonymous$$onoBehavior, hence not letting you use the 'new' keyword. To add it you can use AddComponent() as long as the Base$$anonymous$$ageClass extends $$anonymous$$onoBehavior.
Answer by GregoryNeal · May 01, 2015 at 07:57 PM
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>();
or
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#
Your answer
Follow this Question
Related Questions
Error trying to createMonoBehaviour using the 'new' keyword in cSharp script 4 Answers
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. 2 Answers
MonoBehaviours can only be added using AddComponent(). Alternatively. 1 Answer
Unity - UI Dropdown uses constructor for DropDownItem 0 Answers
Unity 2D rotation speeds problem 1 Answer