4

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 2

12

Small typo, but it happened to me too, so I can relate.

It's MonoBehaviour, as you would say in British English, not MonoBehavior.

https://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html

Sign up to request clarification or add additional context in comments.

1 Comment

Typo indeed! Especially when you create a new script, the template script inserts the correct spelling! Thanks for the reply, and sorry for the dumb question!
1

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.