how suppress must be instantiated using the ScriptableObject.CreateInstance method warning?

In my project I am using:

System.Activator:CreateInstance(Type)

Which throws the following error:

MyClass must be instantiated using the ScriptableObject.CreateInstance method instead of new MyClass.

This there a way to disable this wrong or a way around it from being called while still using System.Activator.

I’m having the same issue

You don’t want to suppress this error but don’t use the Activator. ScriptableObjects (just like MonoBehaviours) must not be created with the constructor. They have a native code counterpart like all UnityEngine.Object derived types. When you create it with new this counter part is missing. That means serialization doesn’t work properly and the instance will become a fake null instance.

As the error said:

MyClass must be instantiated using
the ScriptableObject.CreateInstance
method

If you want to use reflection to create your instances you have two options:

  • Do not derive your class from ScriptableObject and use “normal” classes.
  • If you want to use ScriptableObjects you can simply do a type check before using the Activator. If the type is derived from ScriptableObject ( typeof(ScriptableObject).IsAssignableFrom(yourType) ) you simply use ScriptableObject.CreateInstance instead of the Activator.