- Home /
new Monobehaviour as variable returns warning...
Hi, I have to create an Array with contains a few classes which based on Monobehaviour. This is the code I'm using:
Object[] scripts = new Object[]{Flare, Knife, Speer};
Now I get a warning everytime I start the game:
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 know that a Monobehaviour script has to be attached to a GameObject and can be referenced from there, but now I have a few questions.
Is there a way to use Monobehaviour without a GameObject?
What can happen if I ignore the warning?
Can I just replace Monobehaviour with ScriptableObject?
Thanks for help!
Answer by FortisVenaliter · Jan 27, 2016 at 02:55 PM
No, you should not ever use Monobehaviour without a GameObject, in my experience.
Unexpected behaviour. Most notably, null references for transform and gameObject fields.
Yes, that's a good idea, or just don't derive if it isn't an object in the scene at all.
Your answer
![](https://koobas.hobune.stream/wayback/20220612061933im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My structs are acting like classes in scriptable object. 1 Answer
Adding inherited class to list 1 Answer
How exactly do Monobehaviour functions work? 2 Answers
Null reference.. 2 Answers
AudioClip in ScriptableObject 1 Answer