Finding the source of "You are trying to create a MonoBehaviour using the 'new' keyword"

I joined a project partway through development, and I am trying to go through and remove as many warnings as possible. When running, it outputs this warning once, without a file or line number.

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

How can I locate where this warning is being generated?

To find where the warning is generated you have to scan through all your scripts.

The reason for this warning is greatly explained in the answer to this question about You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed

Basically the reason is as per the answer:

“Unity does not allow you to
instantiate anything inheriting from
the MonoBehaviour class using the new
keyword. This seems a bit odd, until
you consider what the MonoBehaviour
class is.”

So to find this you have to check all your script files that may be accessing other scripts in it and find out where this is done.

  1. To do this you can search for the ‘new’ keyword in your script files and then check if while using new to instantiate are you performing this action with any of the script files.

  2. Try to find the name of each of your script file in the code so that it will give you where the scripts are being accessed in another script.

  3. Or as mattyman said, try to disable the objects and find out the script on which object is causing this issue and then search for where this is done in the script attached to that object.

Which approach to take depends on situation and personal preference.

Best luck!