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.
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.
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.
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.
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.