C# scripts not inheriting from Mono

When ever I create a c# script inside the editor it doesn’t inherit from Monobehavior. This seems to have happened when I switched to using Perforce.

Currently to get around the issue I’m having to create a class in VS then add in everything else.
I’m connected to the server inside of Unity using the built in version control and also connected inside VS.

Any help is appreciated.

What does the script contain when you create it from the editor? Have you modified the script template files?

Everything that a normal script uses. I haven’t intentionally changed anything.

using UnityEngine;
using System.Collections;

public class Name : MonoBehaviour
{
}

But that class does inherit MonoBehaviour. What actually is the issue?

1 Like

When I create the script it says MonoBehavior but it’s white and not blue/green. I’m able to attach the script to objects in Unity but I obviously don’t have access to Mono features such as Gameobject, etc.

1 Like

Anybody else having the same issue? Is this something in the project configured incorrectly? As I said this has only been an issue since using Perforce.

It doesn’t happen 100% of the time.

3055560--229403--VSMonoIssue.PNG

It’s still inheriting, it’s your editor that’s broken. Seems like you’re using Visual Studio?

You should be getting a message up about “files has been modified externally” when you create a script from Unity. Make sure to select “reload all” in that dialogue.

1 Like

I am using Visual Studio.

When this issue happened a few months back I used to reload all to temporarily fix the issue but now I don’t even get the reload window pop up. I have found that in Unity preferences if I change the text editor to mono and back to VS its fixes it. Again this only a temporary work around.

Have you guys found any solution? For me changing editor to MonoDevelop and back to Visual Studio is not working.

Old thread but it doesn’t seem to have the answer I needed and it seems to happen on every new script I add with VS open at the time. What I did that has worked so far was to close and reopen VS, this gets my new script in the Solution Explorer. Then I click the Refresh button at the top of the solutions explorer in VS to correct the problem with monobehaviour not being inherited and made my intellisense work again.

1 Like

It’s an old thread, but I’ve found a solution that doesn’t feel like a hack.
When you have your script open in VS, you will notice it says miscellaneous file in the left top corner of the script window (the window with actual code). That means it doesn’t recognize it as a part of the solution.

You should do the following thing:

  • have that script active (highlighted blue)
  • go to File and pick from the list an option that says Move path of the script.cs into:
  • you will notice the miscellaneous changed to whatever you have your project called
11 Likes

I seem to be having an issue like this as well. In the code it is saying it is deriving from monobehaviour but it doesn’t work when you drag the code to a gameobject, etc.

Oh I fixed it. The file doesn’t change the class name when you change the file.

Thank you so much!!
That worked.

2 Likes

For me the “Attach to Unity” button in Visual Studio worked, mono got default blue back.

edit - See my next couple of posts for better solution.
After following suggestions above, I ended up with my own workaround.

  • Go back into Unity.
  • Right click on script and choose: “Open C# Project”
  • A folder containing the solution and project should open.
  • Double click the .sln file to open it.
  • Your script can be found in the solution explorer window under Assets/Scripts (assuming that’s where you put them).
  • After saving, your problem should be resolved. If you open C# Script files from unity, they should open in the solution with references working.

However, if you open the file standalone again, the problem remains. I’m still working on a fix for that.

1 Like

edit - See my next post for better solution.
A slightly better solution to the one above is to go into Unity->Edit->Preferences…, External Tools and select ‘External Scripe Editor’ and browse for your .proj file. You’ll need to change the drop down list next to file name from .exe to All Files (.) so it shows the csproj file. Select the sln or csproj file and click Open to associate it. Close out of the Preferences dialog and then open your script file from Unity. Now, when you double click on a script file in Unity, it will open the solution and project with references working. However, you may have to manually navigate to your preferred file once it opens. Better but still working on a proper fix.

Seemed to have fixed it. Close any instances to Visual Studio before performing these steps just to be safe.

#1 - Get the full path to Visual Studio (2017 in my case)

  • HowTo: Find the shortcut icon in your start menu and view the properties.You may have to open the files location and then right click and view properties.

#2 - Copy the full file path to your clipboard. Should copy the entire string so you’ve got something like this but yours will likely be different. “E:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe”
#3 - Go into Unity->Edit->Preferences…, External Tools and select ‘External Script Editor’ and paste the path to visual studio without the double quotes and click Open.
#4 - You should see the friendly short name of Visual Studio (2017) for External Script Editor now.
#5 - Close the Preferences dialog and try double clicking your script file. Should open directly into your project/solution to the selected file.

15 Likes

Chiming in here…

Came here trying to fix my issue with this. I ended up needing to install .Net Framework 4.7.2 and open the solution instead of just opening the C# file

This worked for me

(I’m using VS 2019 btw.)

10 Likes