Why when i open a new script in visual studio the MonoBehaviour is not in light blue ?

I marked with red circle what i mean it’s not in light blue:

And marked in blue circle this example to show how it is suppose ot be:

In the editor in the Project > Assets > My Scripts > Ladder > mouse right click Create > C# Script give it the name Raise then double click but i can’t use it since the MonoBehaviour is not active not in light blue.
Other scripts have it fine.

Not sure what is wrong. I tried to give it other names like RaiseLadder or Raise Ladder but each time i create the script it’s not working.

4 Likes

Its a bug with VS and Unity. Sometimes the VS intellisense just breaks. Usually deleting the Library and the .csproj/.sln files fixes the issue. These folders get recreated by Unity upon reopening the project.

6 Likes

Sometimes the .cs file will be opened as a single file that has not been registered properly in the project file yet, hence your assembly references and other source files are not accessible from the newly created script.

I can only speak about personal experience, but for me, this mostly (or even exclusively) happens when I use ‘Enter’ on a newly created script. Double click does usually work like a charm and triggers a solution-reload dialog as soon as VS gets focused again.

Once you run into this problem, it is usually enough to close VS and re-open with Unity. If that doesn’t fix it right away, you still got the option that @TaleOf4Gamers_1 has mentioned.

8 Likes

It caused by Renaming Project Folder after project creation.
Simple Solution: Rename your project file to original.
Boring Solution: After you create your script, go to VS and find your newly created script in the solution explorer panel. Right click and choose “Exclude from Project”. Then right click your Assets folder on solution explorer, select Add context, and then select “Existing Item” option. Then choose and add your newly created script file from this window. That fixes the problem. But still you have to do that for every single script.

2 Likes

Hello. I think this post is relatively old. Please excuse me for my english. I seem to have found the solution to this problem.
Right click Assembly-CSharp in the solution explorer and click unload the project. Then redo the same manipulation but this time to reload the project. Alleluia it works again :wink:

11 Likes

I add this problem today; I just quit both Unity and VS and restarted them and the problem solved itself.

I hope this will be fixed in future versions.

2 Likes

I generally only hit weird issues like this when I create a new CS file and try to open it before Unity has finished recompiling from the change.

I’m afraid this is not the reason; I just created a script and opened it about 30 seconds after creating it and the problem is there. It doesn’t take that long to Unity to compile a new empty script.

Only fix: closing VS and restarting it.

I think generally VS with Unity has bad synchronization.
I didn’t hit issue as yours. But VS desyncing is not something uncommon for me.
So there may be something else going on.

Try in Unity Ctrl+R tor refresh, after script is created.
Or create script from VS. And copy, or type MonoBehaviour manually.

They ve fixed it, download latest Tools for Unity and Visual Studio 2017, there is a post in VS forums stating so. Anyway for me it does not happen anymore.

I’m already using VS 2017 and the latest tools for Unity and the latest 2018.2 Unity: it’s not fixed.

Here is a quicker workaround I fixed.

Problem:
Visual Studio 2017’s intellisense doesn’t recognize new MonoBehaviour scripts in generated from Unity.

Workaround:
Right-click the project in VS and unload the project (shortcut-L), right-click again and load it (shortcut-L again).

All MonoBehaviour scripts created hereafter no longer require the workaround. Visual Studio magically recognizes newly created scripts! I don’t understand why, but it works.

1 Like

It is i! A random Necromancer passing by, i actually fixed this problem in Unity and Visual Studio Link, the monobehaviour not being highlighted.

I fixed it by Unity > Edit > Preferences > External Tools > External Script Editor > (Change it to) Visual Studio 2017 (Community)

then restart both programs.

82 Likes

If I recall correctly, this was happening with Visual Studio already. The problem seems to have been fixed with the most recent versions of Unity; I haven’t seen it happening in 2018 or 2019 yet.

4 Likes

Thanks a bunch for the solution.

7 Likes

Thanks for the solution.but i found before i saw you 3 minute later. T T

THANKS FOR THAT, searched for hours, many thanks!

1 Like

If you don’t have Visual Studio 2017 as an option, go to Start (Win10), type Visual Studio, right click on your preferred version (2017), select ‘open file location’, right click on the ‘Visual Studio 2017’ shortcut and select properties. Copy the full Target path without double quotes. Close out of these windows and get back into Unity.

Unity > Edit > Preferences > External Tools > External Script Editor > Browse

then paste your full path in without quotes and hit Open. You should now see the friendly name for “Visual Studio 2017”. Close out of Preferences and double click your script file. Should open as expected now.

Thanks to everyone that came before me.

2 Likes

This worked for me, didn’t restart unity (VS was already closed).
NB: Using VS 2019

2 Likes

THanks so much dude!!!

2 Likes