Debugging with unity

13,611

Solution 1

Use 'Attach' in MonoDevelop's debug menu; you should be able to attach to the running Unity process that way. (You may need to ensure that the appropriate option is turned on in Unity's preferences).

Solution 2

I've always changed the default script editor (unity preferences -> external tools -> external script editor) to visual studio. This lets you use breakpoints and preprocessor macros to make debugging much easier. When you want to start debugging press f5 in visual studio to connect it to unity then play like normal and if a breakpoint is hit visual studio takes over.

You don't have to change anything else and you won't have to keep opening and closing things like you are now.

Solution 3

Another way to debug is by using the:

Debug.LogError("foo");

or

Debug.LogWarning("foo"); 

Another note is that you can actually bind objects to the Log. This will cause the editor to highlight the object is question in the event you are iterating over a list of GameObjects. i.e.:

Debug.LogWarning("this object broke", gameObject);

If you turn on "Error Pause" in the console window, the game will automatically pause when the LogError is met. But be warned, it will pause whenever an error is thrown.

Solution 4

UnityVS is now officially part of Microsoft and is available as an add-on for vs 2010,12 and 13. Here's the MSDN blogpost linking to the various versions: http://blogs.msdn.com/b/visualstudio/archive/2014/07/29/visual-studio-tools-for-unity-1-9.aspx

Solution 5

Do you know about the Unity "Console" window? You should be able to open it from Menu/Windows/Console. It will act as a debugger giving you errors and warnings both while pre-compiled and at runtime. If I misunderstood the question, let me know.

Share:
13,611
smallB
Author by

smallB

Updated on June 07, 2022

Comments

  • smallB
    smallB almost 2 years

    At the current moment, what I'm doing is that I'm opening Unity, double click on one of those scripts I've written, then MonoDevelop gets opened, now I have to close unity and in MonoDevelop I do Run >> Run with >> Unity Debugger.

    After this Unity gets opened and when I press the play button in unity the debugging session starts. But once only. If I stop this session in either Unity or MonoDevelop I have to repeat this whole procedure all over again, which is very tedious. I have to open Unity, close Unity, (I have to close it because next step which is Run >> Run with >> Unity Debugger will open unity and if unity is already opened I'm getting error saying that only one instance of unity can open one project at a time).

    What I'm asking is:
    Is there any better workflow which would free me from this tedious switching on and off Unity, and every time I stop debugging session I would just start normally without doing these tedious repetitions?

  • yoyo
    yoyo over 11 years
    Yes, if you attach the MonoDevelop debugger to the already running Unity editor then there is no need to close and reopen Unity.
  • 0xC0DED00D
    0xC0DED00D about 11 years
    Attaching to the currently running unity process is certainly the better way, than what OP is doing. I've found it out while trying to debug multiple times, using the same procedure what OP did, and suddenly saw there's another option too. Tried it and it worked.
  • Ender Doe
    Ender Doe over 7 years
    Im surprised that this wan't the default option for smallB.