2
\$\begingroup\$

In Unity 5.2.0f3, when I double-click a C# script, it effectively launches Visual Studio Community 2015 but then opens the source code file in Notepad instead of inside the Visual Studio IDE.

How can I make sure that source code is opened inside Visual Studio (the one Unity started)

Investigation Notes:

  1. It happens both from double-clicking an attached script the inspector or the script icon in the project/assets panel.
  2. Although the specific file is not open, the "Class View" in the VS IDE loads that I'm in MyGame.CSharp and MyGame.CSharp.Editor, so some kind of interaction must happen.
  3. It happened to me that initially Visual Studio claimed it was in "expired trial mode", then I created a MS account and bound it to the the VS. Don't know if this had any effect on that.
  4. I can't tell if it was working previously, as I come from a Unity version that used to use MonoDevelop.
\$\endgroup\$

3 Answers 3

3
\$\begingroup\$

Well, it is not a matter of Unity. In fact, Unity sends the file to the Visual Studio and it is Visual Studio that re-sends it into a NotePad.

Reason

The reason behind is that VS does not open all the files. The file I was trying to open is legacy source code that, for some reason, did contain the ASCII ETX character (0x03) in it.

Visual Studio seems to reject non-perfectly-formatted files.

How to repeat

In fact, the error can be repeted without unity at all and playing only with the filesystem and the Visual Studio.

I just started by making a drag-and-drop from the Explorer into the VS, like this:

Drag and drop of an invalid file from filesystem into Visual Studio

Visual Studio finds the file as "invalid" and does not want to open it. So it launches Notepad:

Visual Studio rejects to open the file and sends it into the NotePad

You can see it directly starts with the text using. This is what the file looked like (dumped with OctalDump dumping "hexa" in one line and "ascii" in the other, in a Linux machine):

Invalid file hex dump

You can see it really starts with the text using at byte 0. You can see the rare character there (0x03, ETX).

I then opened the file into Sublime Text. You can see the ETX there in the middle:

Invalid ETX character seen in Sublime

Solution

What then I did is to tell Sublime to add the UTF-8 BOM header into the file, so it should be, at least, "more compatible" than finding weird characters without telling anything.

Select Save with BOM

This is what the file looks like now in a hex dump:

Weird character in a file with BOM

So you can there see first the BOM header at bytes 0, 1 and 2, then the using word starting at byte number 3. Still you can see the ETX invalid char there inside.

Now, you drag-n-drop the file into Visual Studio, and it opens it:

Visual Studio opens the file

Finally, and for the sake of testing, I just duble-click within Unity and yes, it opens within Visual Studio:

enter image description here

Note 1:

If I remove ALL invalid characters from a file, then, even if the file does not have a BOM header, VS properly opens the file. But as this is legacy code and I don't know why another coder put the ETX there, I can't just remove it and let it go... I need to keep the source as untouched. So adding the BOM was the solving solution.

Note 2:

It happened to me with the ETX invalid char, but I guess this is extensible to any non-printable basic-ascii127 character.

\$\endgroup\$
0
\$\begingroup\$

I found another reason. On the PC where I had Windows 7 I had the Visual Studio devenv.exe program set to open as administrator (every time). Unity was NOT opening as administrator, and as such, when Visual Studio was being launched by Unity, Notepad opened first, followed by a blank Visual Studio window. As soon as I right-clicked the Unity icon and selected Run as administrator it was all good, so I just updated the shortcut properties to always run Unity as administrator (right-click, then Properties->Advanced->Run as administrator).

In summary, if the security context is different, it will most likely cause this issue as well.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This answer worked for me. I had VS 2017 running as administrator so I had to change the others: unity hub -> run as administrator and unity editor -> run as administrator . It worked just fine - THANKS James Wilkins \$\endgroup\$
    – AllaliAdel
    Commented Jul 22, 2019 at 17:08
  • \$\begingroup\$ You're welcome. Don't forget to upvote the answer if it works for you. ;) \$\endgroup\$ Commented Jul 23, 2019 at 19:13
0
\$\begingroup\$

i had this problem with Unity 2020.x and Visual Studio 2019 (happened after testing Rider editor and then switched back to VS)

Solution:

  • Edit Preferences, External Script editor: Set to VS2019 (if not already)
  • Assets/Open C# project (this opens VS2019, and now double clicking scripts also open in VS2019)

*Nevermind: it still opens with notepad after restarting Unity

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .