Unity script MonoBehaviour error

Viewed 10149

I am working on a car driver game in Unity. I was attempting to make some touch screen buttons and adding a C# script for testing mouse and touch input, but I get the console error message

"Assets/Standard Assets/MouseButton.cs(4,28): error CS0246: The type or namespace name `MonoBehavior' could not be found. Are you missing a using directive or an assembly reference?"

Script listed below:

using UnityEngine;

using System.Collections;

public class MouseButton : MonoBehavior {

  void OnMouseDown() {
      Debug.Log("The Mouse is down on " + this.name);
  }

  void OnMouseUp()
  {
      Debug.Log("The Mouse is up on " + this.name);
  }
}   
2 Answers

For me on Windows 10, Visual Studio 2019, and Unity 2020LTS was working:

  1. Check that is actually "MonoBehaviour" and not "MonoBehavior". Or an individual script behavior. If you open a second script MonoBehavior is not marked as red.
  2. If the problem subsists in all scripts, then make a backup (just in case), and In Project right-click Asset and "Reimport All".
  3. If that was not working, and the issue persists, select Edit > Preference... > External Tools > External Script Editor > and switch "Visual Studio.." to "Open by file extension" or vice-versa. Just switching was working. Attention: "Open by file extension" will work without auto-completion.
  4. If you select "Open by file extension" auto-completion will not work. You can try "Visual Studio [version number]". Then go to Package Manager > Packages: In Project > Visual Studio Editor and upgrade to the latest verified version. Remove "Visual Studio Code Editor". And lastly "Reimport All".
Related