Monobehaviour not detected in file

Hi,
I am just starting to learn Unity (just yesterday) …well “trying” to start learning until i got this annoying error.

I just create a C# script in a new project and right from the beginning i get hit by a “all compiler errors need to be fixed …bla bla bla”

even though the project is freshly new, no other scripts are there only this new one. the error keeps popping. if i delete the script everything goes fine. i try creating a new one and the problem appears again. i searched for two straight days for a fix with no results. so far i tried doing this :

  1. made sure the name of the class and the name of the file are exactly same (lower upper case included). keep in mind this happens even if i just click create script with no change.
  2. did the refresh reimport all from the assets menu.
  3. delete all projects and reinstall unity again.
  4. disable antivirus software
  5. restart windows over and over
  6. restart unity and try deleting all projects and documents and create fresh ones.
  7. tried both monodev and visual studio both show no errors in the script.

I am running the latest version of unity (5.6) on windows 10 with latest updates.

Would really appreciate the help on this one. its driving me crazy but i am really into learning this thing.

I think I should add that When i check the console for errors i just find one single red exclamation mark with no text or details. I try to click on it and nothing happens. Try to clear console and nothing happens the red mark is still there

Post the script although I’m guessing it doesn’t inherit from monobehaviour and you are trying to use something which requires it.

I would also suggest reading here to learn how to post the code - Using code tags properly - Unity Engine - Unity Discussions

Thanks for the reply!
Though like i said its the default template that is created when you create a new script file. Anyway below is the script (note that i made sure the class name is exactly as the file name)

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
      void Start() {
      }

      void Update() {
      }


}

Any hep :frowning:

If the autogenerated script doesn’t work, that’s a bug.

It might be that Unity for some reason don’t get to read your script. Try starting Unity as administrator.

It might be that Unity broke something in 5.6, so you could try going to the download archive and download 5.5.

1 Like

@shebli7 recently this started happening to me. I was thinking it was a bug with Unity 2018.26 or some how a bug between an interaction with VS2017 and Unity 2018. I reimported my program and the error went away. However, whenever i added a new script the error would come back.Then I noticed an extra “;” in one of my programs and removed it. The error went away permenately. Hope this helps anyone else who finds this.

Had the same problem.

Firstly, I deleted the meta files of those scripts and reopened Unity, then I changed the name of the class in VS then in Unity and it worked.

Using Unity 2018.3.12f1 with Visual Studio 2017 Community.

Hope this help!

1 Like

I had the same problem using 2019.3 . So I copied contents of the script to Notepad. Deleted the script and created a new one. When I pasted the script back from Notepad the problem happened again. So I then cleared the script and pasted back from Notepad one line at a time and it worked!

I tried this several times and am convinced there is some hidden formatting which was corrupted.

So if you have this I would first try copying everything out to clear your script, then paste back line by line checking each time.

I had the same problem in Unity 2020.1.14f and my solution, bu surprise, was to create a new script. When I did that the problem disappeared totally.

3 Likes

After updating from Unity 2020.3.7f1 Unity 2020.3.6f1 I had the same problem. None of my scripts using Monobehaviour found that class.

Microsoft Visual Studio Community 2019, Version 16.9.4

I have 20+ scripts in my project.

I just added a new (empty) script file via the Unity Editor and that fixed the problem. Now the code builds fine. I could remove the empty C# script file and everything still was working.

I think this must be a bug. A missing reference or confused metadata file.

That’s not updating, that’s downgrading. And Unity doesn’t support any form of it. You always need to revert your project in your version control system or load back a backup. And as the update alert dialog says always backup your project before you update it to a new version. It is not a joke.

Never use space in scripts name!

‘My Script.cs’ Not work.
‘MyScript.cs’. Work Normaly

Exatamente, obrigado

worked for me!

Open Edit->Preferences->External Tools change the external script editor back and forth and then click “Regenerate Project Files”
that was the thing that worked for me

2 Likes

Hey everyone, so I actually tried all the above solutions and none of them worked. But what did work for me was simply going checking the Unity extension in the VSCode Extensions tab, and enabling it for the current workspace. After you enable it, a popup should appear on the bottom right asking you to restart the language server, and once you do your references will now be found.

1 Like