32

I'm using Visual Studio with Unity. In general in VS I can simply click "Attach to Unity" and it will build the solution and indeed attach to Unity.

Sometimes, however, a project loses the "ability" to do so, what means that suddenly clicking the button will only build the solution and not attach to Unity.

I really would like to know what's happening there as I'm not aware of doing anything that could result in such a behaviour.
There are some parameters that might have an effect:

  • it happens only for some Unity projects: a project is either "infected" or it happens never (on my watch)
  • if it starts happening it will always happen until I reboot the PC (closing VS and Unity is not enough)
Benjamin Zach
  • 1,452
  • 2
  • 18
  • 38
  • What do you mean infected? With malware? I'm starting to doubt my project when you said that. – Leandro Mar 20 '22 at 08:36
  • I'm using the term "infected" not in the sense of malware here; hence the quotes. I only wanted to express that if it happens to one project it will stay so and therefore never rework correctly again by itself. – Benjamin Zach Mar 21 '22 at 07:34

13 Answers13

37

For me (Unity 2020.x and VS19) this happens quite often. Simply close VS and in Unity > Edit > Preferences > External Tools

Click on "Regenerate project files" (you don't need to check anything else, just click the button)

Start VS again, and it should work as normal.

ballade4op52
  • 2,142
  • 5
  • 27
  • 42
Delta
  • 721
  • 8
  • 5
27

For me "Attach to Unity" disappeared from the toolbar.

enter image description here

But I still have a button in Debug -> Attach Unity Debugger

enter image description here

Hope it will help someone.

Visual Studio Community 2019 16.7

Unity 2020.1.2f1

hiichaki
  • 834
  • 9
  • 19
  • for me the same, but where is `Attach to Unity` button? – Jovan Oct 13 '20 at 22:31
  • super helpful, thank you. @Jovan the item in the menu was all I needed. Once it's attached, just run your app from inside the Unity Editor and VS will do its breakpoint thing. – orion elenzil Nov 13 '20 at 22:49
10

To prevent this from happening: When there are compiling errors in your code, don't double click on Unity Debug Log line, which opens VS at the line that threw the error, but it messes with the project files.

Regenerating the project files under Unity > Preferences > External Tools > Regenerate Project Files. Also make sure that Visual Studio is set under external tools, as well as all the Unity dependencies are correctly installed via the Visual Studio installer.

enter image description here

Visual Studio will prompt you to reload the project, optionally rebuild your entire solution in VS and restart both Unity and VS.

Pfinnn
  • 439
  • 8
  • 18
4

Delete all visual studio related files from the project folder. sln, csproj, vs (hidden) folder.. Reopen project with unity hub, edit one of script and all files will be generated and you will see Attach to Unity button again.

Mustafa Ekici
  • 7,263
  • 9
  • 55
  • 75
3

This is a quite common issue when using Unity. Me and my colleges have experienced this a couple of times each month. It usually automatically "repairs itself" after restarting VS/Unity or in worst case rebooting the computer.

Now, I've got this problem again with latest VS2019. I've tried:

  • Generate a new clean project
  • Rebooting
  • Regenerated all visual studio files
  • Reinstall VS 2019 (clean)
  • Reinstall Unity (It is clean - I'm only using one! editor version)
  • All the above suggestions

Are there any network-related issue to this or how does the attach to Unity works ? Can I check some log-files related to this?

Last time this happened to me, I needed to reinstall Windows. I might be in that situation again.

enter image description here

Fam Wired
  • 157
  • 1
  • 6
  • Which Unity version do you use? – Benjamin Zach Feb 07 '22 at 07:48
  • 1
    I've tested with at least 5 different Unity versions ranging from 2019.3.26 to 2020.3.27. I tried to uninstall the software completely (Both Unity/Unity hub/VS2019) It does not work with latest beta of VS2022. I suspect a network-related issue but I have unblocked them in the wf.msc, both inbound and outbount rules. – Fam Wired Feb 08 '22 at 10:34
  • Can I inspect any log-files or attaching a debugger to devenv.exe ? – Fam Wired Feb 08 '22 at 10:38
  • 2
    It seems drastic that I need to reinstall Windows again to be able to debug my applications. – Fam Wired Feb 08 '22 at 10:38
2

I had a similar issue, underneath the file tabs does it say miscellaneous files? To fix it I right clicked the Assembly C-Sharp in the solution explorer and rebuilt the project as shown in the picture.

Hope this helps.

Jambla
  • 368
  • 1
  • 5
  • 16
2

Normally just restarting visual studio is sufficient, this issue was addressed and fixed by MS in VS around early december. Previously it happened each time you made a new script file.

Make sure you have installed the latest visual studio patch, as well as the "visual studio tools for unity"

BugFinder
  • 17,474
  • 4
  • 36
  • 51
1

I had this issue once, what fixed it for me was reconnecting Visual Studio to Unity Edit > Preferences > External Tools > External Script Editor

Also check the correct version of Visual Studio is listed there.

HoloLady
  • 1,041
  • 1
  • 12
  • 28
1

For Unity 2020.1.1 and above
in VisualStudio19:
go to "Build" -> "Rebuild Solution"

enter image description here

1

Unity > Edit > Preferences > External Tools > Editor Attaching

baby lulu
  • 31
  • 5
1

Sadly, I don't have an answer yet, but I can identify the symptom pretty accurately. If you run Unity and then run TCPView (from Microsoft) it will show you the UDP port that Unity.exe is listening to. Sometimes Visual Studio will be off by a few when trying to connect, and of course if it's trying to connect to the wrong port, it'll never work. See this image:Wrong ports being used.

According to one of the Unity developers, Unity.exe advertises the debugger port with a multicast on a specific port, but it's obviously got a bug. Sometimes Unity says it's unable to use a specific port, blames other processes or firewall rules. I'm watching in TCPView, nobody has that port and my firewall is disabled. It's just buggy and getting some error it doesn't handle properly. No answer here, just data. It's been only semi-working for years.

Jason Hughes
  • 2,612
  • 1
  • 10
  • 10
  • Great insight, thank you. I think that's really the point. – Benjamin Zach May 15 '23 at 07:38
  • I've had only one connection work in the past four days since the windows update. Destroyed my productivity. So I grabbed the code from the visual studio integration package and added debugging prints to the console. The exact error in my case is SocketException "Permission denied. An attempt was made to access a socket in a way forbidden by its access permissions." – Jason Hughes May 16 '23 at 09:40
0

In my case (Unity 2020.2, VS Code 1.53.2) Attaching Debug option is missing on Unity.

After restarting my macbook,

  1. Open VS Code

  2. Open Unity

  3. In VSCode-> Run -> Start Debugging enter image description here

  4. Unity show up to me alert just like below. enter image description here

I choose Enable debugging for all projects option.

I hope, It will help someone.

Emre Gürses
  • 1,992
  • 1
  • 23
  • 28
0

Check that the correct process is selected in Visual Studio's "Attach to Process" dialog.

When you click "Attach to Unity", a "Attach to Process" dialog should appear in Visual Studio. Make sure that "Unity.exe" is selected as the process to attach to.

If the "Attach to Process" dialog does not appear, you can find it in the dropdown menu under "Debug".

MattSt
  • 1,024
  • 2
  • 16
  • 35