- Home /
static-function new() MonoBehaviour warning
Hi,
I am coding a static function, and inside I am working with some general variables, so I am getting 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".
So I found this previous answer but in my case I am working with a static function, so I think it makes no sense to add the script to 'someGameObject'
var thisInstance = someGameObject.AddComponent(MyScriptName);
Now, I am a bit lost, as you can see, any help will be very helpful.
Thanks in advance.
Answer by Mike 3 · Apr 12, 2011 at 07:38 PM
It either needs to go onto a new gameobject, or needs to not be a monobehaviour
var go = new GameObject("Your Script Container");
var thisInstance = go.AddComponent.<MyScriptName>();
or
class MyClass { //code }
//somewhere else var whatever = new MyClass();