Debug Unity using Visual Studio and VS-Code
If you are on Windows or Mac there is an efficient way to debug your code at runtime and set break points in the source code, watch local variables and check the stack trace using vs-code and visual studio.
Make sure you have install the visual studio tools ( this apply for both vs-code and vs studio) in Unity Hub.
Go to Unity Hub -> In the left panel select Installs -> select the Unity Version you are working with -> click on the configuration wheel -> add modules
Make sure the Visual studio tools check box is enabled. If not, check it and install the package.
2. In your visual studio installation make sure you have the Unity Dev-Tools is installed.
Go to your Visual studio installer, select the version you are using and select "Modify"
Make sure the Unity Dev-Tools check box is enabled. If not, enable it and install the package
3. Go to your unity scene ( you might have to restart the app after installing the mentioned packages ) and select any script you want to debug, if you double click it, visual studio will open the .cs script. Make sure Visual studio is the predefined text editor
In Unity go to Edit -> Preferences -> External Tools. Select Visual studio as the external text editor
4. Once you are in visual studio, place a break point any where in the source code and press the Attach to Unity button
5. Go to Unity and run the scene that contains the script you opened on visual studio. When the application reach the break point you will notice that Unity stops the scene execution. That is because the Visual Studio debugger took control of the application. Go to visual studio and you will see the break point line executed.