Visual Studio loses ability to attach to Unity, why?
Asked Answered
S

15

34

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)
Samaria answered 25/3, 2019 at 11:47 Comment(2)
What do you mean infected? With malware? I'm starting to doubt my project when you said that.Bathetic
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.Samaria
S
40

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.

Sodality answered 21/11, 2020 at 11:22 Comment(4)
Unfortunately, this didn't work for me. Using Unity 2019.4.x and VS 2019.Boldface
I've noticed that I was runninv VS 2019 Version 16.8.2. After updating to 16.8.4 and restarting Visual Studio, it worked again. What a funny mess.Boldface
I had to restart Unity after and it worked (unity v2022.2.1f1, vs2019)Woodcock
I just deleted the generated vs solution file, restart vs and it started to work again.Panpipe
M
30

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

Mozart answered 12/8, 2020 at 21:24 Comment(2)
for me the same, but where is Attach to Unity button?Climax
super helpful, thank you. @Climax 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.Meletius
T
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.

Trochlear answered 11/1, 2021 at 17:17 Comment(3)
Worked for me after trying many different things. Thanks!Agulhas
Thank you it fixed the problem for me! =DMonocarpic
no problem, its a common fix for alot of problems that can occur with your project.Trochlear
O
5

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.

Osis answered 13/10, 2020 at 17:12 Comment(6)
This is the one that worked for me! Though there is no longer a "vs" folder. Deleting the .sln and .csproj files solved the problem.Aorta
@MatheusRocha vs is hidden folderOsis
No need to delete csproj files. Just add a new component (script) in the Unity editor and it will rewrite the projects.Shuster
@MustafaEkici I have the explorer showing hidden files and folders, there was no vs folder for me. The solution worked nevertheless.Aorta
This is what helped me.Strawberry
Yup, this fixed it for me too. once I the sln was gone, i restarted VS and the Unity prj. worked like a charm.Salubrious
F
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

Falsework answered 4/2, 2022 at 11:49 Comment(4)
Which Unity version do you use?Samaria
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.Falsework
Can I inspect any log-files or attaching a debugger to devenv.exe ?Falsework
It seems drastic that I need to reinstall Windows again to be able to debug my applications.Falsework
E
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.

Extrusive answered 25/3, 2019 at 12:54 Comment(1)
Rebuilding the solution or the project does not resolve the problemSamaria
F
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"

Forsooth answered 25/3, 2019 at 13:45 Comment(4)
Unfortunately even with the latest VS patch and the 'visual studio tools for unity' this keeps happening and restarting VS for me is not sufficientSamaria
Everywhere else this issue seems to have died down, which version of vs and unity are you usingForsooth
VS 15.9.17 and Unity 2019.1.11Samaria
It would suggest something about your machine is weird, or you have some form of plugin messing it up for youForsooth
C
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.

Chavira answered 25/3, 2019 at 15:14 Comment(0)
S
1

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

enter image description here

Shira answered 10/9, 2020 at 19:44 Comment(1)
Please be a bit more descriptive with the answers. Many will read this answer for a long while.Dactylography
U
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.

Understanding answered 15/5, 2023 at 4:53 Comment(2)
Great insight, thank you. I think that's really the point.Samaria
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."Understanding
D
1

Deleting the hidden .vs folder + visual studio project and solution files didn't work for me.

Restarting windows-explorer allowed me to attach the Visual Studio. (See variant 3)


variant 1:

But when I opened Task Manager and went into Details tab, I saw 3 Unity.exe and 6 Unity Hub.exe.

I ended all of these tasks, and ensured the visual studio files mentioned above are deleted. Restarted unity, clicked on one of my scripts, it launched visual studio, regenerating its files, and then debugger was attaching again!

So check if there are several exes running.


variant 2:

I noticed that my other unity-projects work just fine, VisualStudio attaches to them correctly. Therefore tried this and it worked:

  • I closed unity, visual studio, UnityHub.
  • did what's described in variant 1
  • changed the name of the unity project's directory.
  • deleted the .vs folder + all vs solution and vs project files.
  • Opened UnityHub, removed the old project from the list, and re-added a new one (from this modified directory).
  • It opened, I clicked on one of my scripts, Visual Studio opened and was able to attach.

So the path to unity project is important and is probably cached somewhere. So if visual studio doesn't attach, try changing the path to your unity-project.

in my case it was simply:

C:\myFiles\myFancyUnityProject\ 

renamed to

C:\myFiles\myFancyUnityProject-1\

variant 3: Noticed that I couldn't rename the folder as if something in Windows was holding it. So I opened Task Manager, went into Details tab. Found explorer.exe right clicked and ended the task.

Then clicked File->Run new Task and typed explorer.

Restarting windows-explorer this way actually allowed me to attach the Visual Studio. And I didn't have to delete the .vs folder or any solution files. But I still restarted windows and Visual Studio and terminated stuff as in variant 1.

Department answered 23/10, 2023 at 17:40 Comment(0)
A
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.

Azygous answered 2/3, 2021 at 21:49 Comment(0)
S
0

Unity > Edit > Preferences > External Tools > Editor Attaching

Southing answered 11/10, 2021 at 11:30 Comment(0)
C
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".

Cabalism answered 8/5, 2023 at 11:42 Comment(0)
S
0

Based on this answer I figured that in my case it was somehow related to network adapters Radmin VPN and vEthernet (Radmin VPN). So try disabling all network adapters except the one you are using to connect to internet.

Stand answered 13/10, 2023 at 1:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.