No MonoBehaviour scripts in the file : Going crazy !!

Hello everyone,

I’m really going nuts over this, I have spent hours trying to fix it. At this point, it seems that I have one problematic script, a singleton base class that I basically stole from a unity course :

using UnityEngine;

public class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
    private static T instance;
    public static T Instance
    {
        get { return instance; }
    }

    public static bool IsInitialized
    {
        get { return instance != null; }
    }
    protected void Awake()
    {
        if (instance != null)
        {
            Debug.LogError("[Singleton] Trying to instantiate a second singleton");
            Destroy(gameObject);
        }
        else
            instance = (T)this;
    }

    protected void OnDestroy()
    {
        if (instance == this)
            instance = null;
    }


}

The class seems to work and I use it with derived classes, but I still get the “No MonoBehaviour scripts in the file, or their name doesn’t match the file name”, and I really don’t get the problem.

Another issue with the project is that when I import assets that work fine on other projects, the scripts just don’t get used, I get a “The associated script cannot be loaded” error on the objects. I’ve read tens of forum posts and can’t seem to get a solution… I think the two are linked but I can’t be sure.

I don’t have any console errors apart from
“GetLoadedImportedAssetsAndTargetHashes called with invalid targetHash”
“Assertion failed on expression: ‘it->second.IsValid()’”, which from my research is linked to hdrp pipeline and not related…

Any idea, something wrong with the code, with the project ?

That usually means one of three things:

  1. The class name doesn’t match the script filename
  2. The script doesn’t have a MonoBehaviour-derived script in it.
  3. You have other compiler errors you must resolve first, open the console and check for those.
1 Like

Unity cannot add scripts as components of gameobjects unless it can complete a compile of the code successfully. A compile error of course prevents the compile from completing successfully (obviously). So you must resolve all compile errors in your entire project first before you should expect attaching scripts to gameobjects to work correctly. That likely includes this error you’ve posted, even though it doesn’t come from this specific script, even if it doesn’t come from a script you wrote yourself, even if it comes from a package from Unity; all compile errors must be resolved first regardless where they come from.