How Can We Debug Our Unity Project on Android Device with Visual Studio?
Introduction
In this article, I will tell you how we can debug our Unity projects via Visual Studio while testing it on our Android device.
Let’s start.
- We connect the Android phone to the computer with a USB cable. USB debugging feature must be active. If not:
- Go to Settings > About.
- Click on Build Number 7 times. A message will appear stating that it will enable it.
- Go back and go to settings > Developer options.
- Enable USB Debugging.
Note: It may vary slightly from phone to phone but it is very similar.
2. Mark the lines we want to debug in Visual Studio.
3. In the Unity editor, go to File > Build Settings and enable Development Build, Script Debugging, select the device you connected then click run and build.
4. After the game build is completed, we click on Debug>Attach Unity debugger in visual studio. Then we select the project&device. Keep the machine IP and port number in mind.
5. We click on attach to Unity and select the device. The same machine can be multiple times each time you build. So, we select by machine IP and port.
That’s it.
After the steps we have done so far, it should work, but I saw a problem and its solution on the internet that prevented it from working. I want to talk about it in case it happens to you too.
The problem is that when selecting the device in step 4, the device does not appear. This problem occurs especially if more than one Android SDK is installed on your computer. The solution to this is as follows:
- In the Unity Editor, we select “preferences” from the “Edit” menu. We copy the location of the SDK from “External Tools” here.
- Now go back to Visual Studio and go to the Tools menu and select Options. In the Options window, select Tools for Unity as shown below and in the Debug section you will find “Preferred Android SDK Root”. It can be empty like in my case or populated with any other SDK location. Paste the Unity Android SDK path you copied earlier into this text box. Click OK.
After this if you open Debug -> Insert Unity Debugger again in Visual Studio it should show the Android device. In my case it wasn’t showing, so I restarted the Unity app inside the Android device and then it showed in Visual Studio. You may need to restart the Android app or Visual studio.
Conclusion
As a result, maybe you were writing logs for every line when you didn’t know how to debug like me. But after discovering an easier and cleaner way to debug with this article, I wanted to share it with you.
Bug-free coding for everyone :)