'Unity3d no Monobehaviour scripts in the file, or their names don't match the file name

Hello everyone I've been working on my first game and suddenly I got this error and cannot run the game, I already installed the new version of unity but it persists. I see it can be caused for several reasons but had no luck so far, do you know the most probable causes and how to fix it?

When I select the scripts the only one where I do not get this error is the following:

using UnityEngine;
using Assets.Code.States;
using Assets.Code.Interfaces;

public class StateManager : MonoBehaviour 
{
    private IStateBase activeState;

    void Start () 
    {
        activeState = new BeginState (this);
    }

    void Update () 
    {
        if (activeState != null)
            activeState.StateUpdate();
    }
    void OnGUI()
    {
        if (activeState != null)
            activeState.ShowIt ();
    }
    public void SwitchState(IStateBase newState)
    {
        activeState = newState;
    }
}

But for example here I get the error:

using UnityEngine;
using Assets.Code.Interfaces;

namespace Assets.Code.States
{
    public class BeginState : IStateBase
    {
        private StateManager manager;

        public BeginState (StateManager managerRef)
        {
            manager = managerRef;
            Debug.Log ("Constructing BeginState");
            Time.timeScale = 0;
        }
        public void StateUpdate()
        {
            if (Input.GetKeyUp(KeyCode.Space))
            manager.SwitchState(new PlayState (manager));
        }

        public void ShowIt()
        {
            if (GUI.Button (new Rect (10, 10, 150, 100), "Press to Play"))
            {
                Time.timeScale = 1;
                manager.SwitchState (new PlayState (manager));
            }
        }
    }
}

And so on with every other script.

I've already installed a newer version of unity3d, uninstalled the antivirus, checked the file names but the error persists. I also do not have any class in a namespace..



Solution 1:[1]

I had a strange variation of this issue, and did not see the solution posted anywhere else online.

TLDR

I had whitespace in the namespace for the classes which Unity could not identify as MonoBehaviours.


Background

I had the No MonoBehaviour scripts in the file or ... warning text on two script files in my project, one a MonoBehaviour, and the other a ScriptableObject. Each issue had different symptoms:

MonoBehaviour

  • The MonoBehaviour class was named like MyMonoBehaviourService.
  • The script file was named MyMonoBehaviourService.cs
  • The class was not partial, it had no nested types, nor any other types defined within the file.
  • I could not attach MyMonoBehaviourService to a GameObject via the inspector, but I could via gameObject.AddComopnents<MyMonoBehaviourService>(). This resulted in the script attached to the object, looking and behaving correctly.
  • Upon entering PlayMode, I would get the error: The referenced script (Unknown) on this Behaviour is missing!
  • After exiting PlayMode, the GameObject with the MyMonoBehaviourService attached would now say that it was missing.

Scriptable Object

  • The ScriptableObject class was named like MyScriptableServiceData.
  • The script file was named MyScriptableServiceData.cs.
  • The class was not partial, it had no nested types, nor any other types defined within the file.
  • I was able to create a MyScriptableServiceData via C# and save it using the AssetDatabase.
  • I was then able to see and modify the properties using the inspector.
  • Upon exiting and re-opening the project, the asset would say that its script was missing.

Resolution Strangely enough, the issue was actually whitespace in the namespace for each class. I mistakenly thought I was seeing word-wrapping but actually, there was a newline in the namespace.

namespace Appalachia.Prototype.KOC.Areas.DeveloperInterface.V01.Features.PerformanceProfiling.Services.
FPS
{
    public sealed class FPSProfilerService : MonoBehaviour
    {
    }
}

needed to be changed to this:

namespace Appalachia.Prototype.KOC.Areas.DeveloperInterface.V01.Features.PerformanceProfiling.Services.FPS
{
    public sealed class FPSProfilerService : MonoBehaviour
    {
    }
}

I'm not sure why this causes an issue with MonoBehaviour/ScriptableObject detection but it really took a while for me to uncover.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris Schubert