Tip - Using correctly non-Mono-inheriting classes

In this lecture, the code used to access the ActionMaster class gives a compiling warning:

“ActionMaster must be instantiated using the ScriptableObject.CreateInstance method instead of new ActionMaster.”

That is, you should not use new ActionMaster(); when creating an instance of the class.
Of course, even on Unity 5.5, the code works even if you get the warning, but it’s best practice to conform (Borg style :D) to the Unity requirements.

So, how to do this? It’s actually pretty simple.

Firstly, when creating a new class which doesn’t inherit Monobehaviour, you should always make your class inherit the ScriptableObject class, like this:

public class MyClass : ScriptableObject {//mycode}

Once you’ve added the : ScriptableObject class extension, you can create an instance of your class this way:

MyClass myClassInstance = ScriptableObject.CreateInstance<MyClass>();

This way, you won’t get anymore the warning. For more in-depth information about Scriptable Objects, you can check the tutorial on the Unity webpage.

1 Like

Thank you for this, those warning were getting under my skin;).

Privacy & Terms