Using "new" to create a Class

I have a very simple class in my program. In another script, I create an array of this class.

public MyClass[] Classes = new MyClass[0];

Now, of course, I’m getting the warning “You can’t use the ‘new’ keyword to create a MonoBehavior”, etc. (A warning that I apparently can’t suppress.)

However, the program runs fine, and its my understanding that this would only become an issue if my class was “extending MonoBehavior”, which its not. It’s literally just a handful of string and integer variables.

Am I correct on this? As I said, the program is running fine, and I’m getting the correct output I’m looking for. I’d just like to know exactly what “Extending MonoBehavior” means specifically.

You’re instantiating some class that inherits from MonoBehavior.

Are you sure no class doesn’t inherit from MonoBehavior?

It would look like:

public class WhateverClass : MonoBehavior

Correct. Mine looks like this:

public class EventData

And yes, some of my classes to inherit from Mono, but not the one that I create with “new”.

and the error points to this line of code where you are creating an array of this class that doesn’t inherit from monobehavior?

This shouldn’t be happening. It thinks it’s inheriting from monobehavior, but you say it’s not. There’s not much else we can do for you.

Hi

I’d hazard a guess that your problem is that any script is automatically derived from monobehaviour and so extends monobehaviour.

See here “MonoBehaviour is the base class every script derives from.” . Unity - Scripting API: MonoBehaviour

What this means in practice is that if create a script SomeThing.js, then define public class SomeThing { } within that script, you have already ensured that the SomeThing class is derived from MonoBehaviour.

I believe a way to avoid this would be to derive from something else e.g.

public class SomeThing extends ScriptableObject { }

Have a look at the ScriptableObject class for further details.

Hope this helps, sorry if I have got my OOP terms confused.

Well, like I said, the program is working fine; and its simply a middleware tool to use in-house.

I was just wondering if there was something I was missing. It’d be nice to know for future use.

I have to agree with LordOfDuct, are you SURE that the error is pointing at that line of code? Even if “MyClass” was a monobehavior, you’re not instantiating that class, you’re just instantiating an array, which is not a MonoBehavior.

Note, OP was writing what appeared to be C#…

the preceeding type declaration is why I believe as such:

//OP's code
public MyClass[] Classes = new MyClass[0];

//C#
int i = 5;
int[] arr = new int[3];

//US
var i:int = 5;
var arr:int[] = new int[3];

and as your supplied links states:

Yes, I’m using C#.

The actual warning message I get doesn’t specifically point to anything in my code.

C:/BuildAgent/work/blahblahblah/Runtime/ExportGenerated/Editor/UnityEngineMonoBehavior.cpp at line 19

So how do you know it’s this class causing the issue?

Cuz its the one I created using “new”

EDIT: Nevermind, I found it. I was, in fact, creating a MonoBehavior class using “new”. I changed it to ScriptableObject, moved it to the Start function, and it seems to be working with no warnings.

Thanks!

I was going to say, you sure this is the ONLY new. Cause new is used a lot in C#… I’d be surprised you only used it once.

Good that you found it.

Read the complete warning text, most of the time it lists the detailed stack trace, so your cs file should be in there.

If it’s not, try commenting out ONLY this line (at least the assignment part of it) to see if the error vanishes. My guess is it won’t because your code appears to be valid as-is.