Unity freezes on Debugging with Visual Studio

Hi everyone!!!

I need help to solve this problem:

I have a project that was running fine so far, but since 3 days ago, every time I hit the button in Visual Studio to debug my code, Unity freezes and doesn’t respond. It only happens to me with this project.
Sometimes this windows appears and it takes forever to wait… or at least until I get bored and desperate and stop debugging in VS. But I have waited more than an hour on one occasion to try…

7920013--1010647--TickGlobalCallbacks.jpg

7920013--1010662--TooltipTick.jpg

I have searched on google but I have not found anything that helps me to solve it and I have been 3 days without being able to work, wasting all the time trying to debug my code, but without success.

I have tried different versions of Unity 2020.3.x, 2021.x, etc but still the same.
I have also installed VisualStudio 2022, but the problem is not solved either.
I have tried deleting the “Library” folder from the project so that Unity would regenerate it again. But it hasn’t solved the problem either.
I don’t know what else to do or how to fix it.

Can anybody help me?

Greetings and thanks in advance

If you just run your game in play mode in Unity, does it run correctly? That is, don’t use the VS debugger.

1 Like

Yes. works fine without doing VS Debugging

If you’re using source control, you could try rolling back.
Another possible option is try deleting the solution files and letting Unity regenerate them.
Also make sure that VS is listed under preferences as your external editor.

But, if you aren’t using any source control, make sure you either do so or do some sort of backup before trying anything.
If the debugger worked fine before and now doesn’t and it works fine in other projects, you’ll need to see what you changed 3 days ago.

1 Like

Hi again and thanks for the help!
I’ve deleted the VS solution files and reopened the project to have it build them again.

But the problem has not been solved.
Any other ideas or tips?

Another thing I’ve noticed is that when I get Unity to unlock by waiting many minutes since I turned on debugging in VS, every time I add or remove a breakpoint in VS, Unity freezes again for about 40-50 seconds. Even if I add or remove breakpoints without play mode.

Just to confirm this is the editor that you are attaching the debugger to? Does the debugger attach successfully and you can pause execution to inspect callstacks and whatnot? You might be able to figure out where things are stalled by doing this. As others have mentioned if this just started in the past few days with only code changes to the project it would indicate that there may be some thread that is failing to end on domain reload causing the hang.

1 Like

Sometimes I’ll get that and have to “Delete All Breakpoints” in VS before it will run Attached.

3 Likes

Yes.

@Alex-Thibodeau For some unknown reason yesterday and today I have been working on the project and it seems that everything has worked noticeably better. The problem has not been so serious and in a matter of 1~2 minutes Unity was unlocked and could do the debugging.

Perhaps the improvement was when I restarted the PC after Unity regenerated the VS solution that I deleted.

I’ll keep watching…

As someone else has mentioned if you have a lot of existing breakpoints it can cause behavior like this as well. The the debugger has to jit and then resolve all of them which can be slow.

1 Like

A bit late, but I hope this helps:

For me, the issue was that the unity services core module was updated in my last commit, from 1.4.2 to 1.4.3.

The text below is from Packages/packages-lock.json

"com.unity.services.core": {
      "version": "1.4.3",

To fix it, I closed vs and unity, deleted all the .csproj files, .sln files, and the .vs folder in my project and re-opened Unity. Once these were regenerated, I could use the vs debugger again.

I was following this Microsoft docs page on troubleshooting.

2 Likes

Same happened here, i only had like 2 breakpoints, but when i deleted them the issue was fixed. There is prolly a bug in the parsing of breakpoints that hangs the editor forever. Keep in mind that restarting windows or power cycling it did not solve the issue.

2 Likes

But you are aware that when your game executes and the debugger actually hits a breakpoint or you press pause inside VS, the Unity editor is expected to not respond. Unity runs in the main thread when running in the editor. So when the debugger halts the main thread in order to debug it, the Unity editor application can not respond anymore until you resume execution. Of course break points can immediately break the execution again.

Yes but the breakpoints weren’t hit, and execution wasn’t stopped by VS (at least not i a way I could control it). I use VS and c# since 2005 across all platforms so i’m pretty keen on the basics.

1 Like

This has been an on again again issue for years with Unity but it does seem much worse with more recent updates.

For note if your are patient it will eventually catch up and move on but ya its not a good work experience not even a little

Visual Studio debugging was the best way to debug Unity projects but its just gotten really clunky and error prone

2 Likes

Same issue for me with Unity 2021.1.21f1 and VS. I am having just one breakpoint and this isn’t hit, unity just freezes. Sometimes I don’t even get the loading bar, but after reading the tips here I waited 1 minute after starting the game, then eventually the breakpoint is hit, and I can debug. Several days ago it happened just sometimes, now it’s happening every time. Btw., I can’t even press PrtScn (print Screen) on the keyboard, It wont work either.

1 Like

Just resolved this issue myself, and it was because of a conflicting breakpoint and watch expression - the watch expression would have hit the breakpoint, and unity hit the breakpoint, leading to a deadlock. Clearing all the watch expressions resolved the issue for me.

1 Like