Search Unity

Debugging

Discussion in 'Scripting' started by podperson, Jun 9, 2006.

  1. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    What support is there for debugging scripts?
     
  2. Bampf

    Bampf

    Joined:
    Oct 21, 2005
    Posts:
    369
    Check out the Debug class in the scripting documentation. You can tell Unity to pause via Debug.Break(), and you can log a message to Unity's console.
    You can also dray lines or rays into the scene view, which can be useful for debugging vectors and other things as you compute them.

    http://otee.dk/Documentation/ScriptReference/Debug.html

    One thing that is not clear in the docs though: execution will pause when you Log or Break, but it won't stop right on that line of code. Rather, the engine will stop after that frame is drawn (or maybe once your top-most function in the calling stack return, I'm still not clear which.) So if you put a break deep in a recursive function it won't actually break until the recursion is all done.

    Note that when you click on a Unity console message, whether it was generated by the engine or whether you did it via the Debug class, Unity takes you to the line of code, and the console also displays the stack trace for that message as well. Handy!

    Of course you can also inspect game objects and their components in the editor. By declaring public member variables in your classes you can see those in the inspector as well.

    The Unify wiki has a debugging class that someone wrote. It creates an in-game scrolling text console in your game. You could use this to debug your game even while running it outside of the editor, for instance. I haven't tried it myself...

    These are the techniques I am most familiar with. Hope this helps.
    Matt
     
  3. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Hey, I wrote the debugging class Bampf is talking about (http://unify.bluegillweb.com/scriptwiki/index.php/DebugConsole).

    I wrote it because I was trying to debug many levels of logic, and only being able to see one line at a time in the built-in console (without pausing your game and clicking on the console that is) annoyed me. With it you can display as many lines on screen as you want in whatever color. But it is still just a console logging tool.

    Unity's debugging functions are really good as Bampf said, but most of the time you will be writing your own custom debugging classes anyway. Anything you want to write you can, so your debugging system can get as complex as you want it. (I am planning on adding console input to my system sometime for example. So the sky is the limit.)

    HTH,
    -Jeremy
     
  4. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Better than I feared, worse than I hoped :)

    Thanks for the pointers.
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    One more excelllent debugging tool is private variables.

    In the "expert view" in the inspector, private variables are shown and they live update when you modify them from a script.

    It's almost like a realtime updating debugger, you can track your variables live as they change.
     
  6. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Thanks again!
     
  7. firas darwiche

    firas darwiche

    Joined:
    Oct 4, 2006
    Posts:
    130
    sorry for the inconvenience guys, but what is the expert mode/view.
     
  8. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    The inspector has three levels that can be toggled by tabs at the top. These are Simple, Full, and Expert. They control the amount of information that is revealed about selected objects.
     
  9. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    I think you mean "Simple, full and debug", so "debug" is expert then?
     

    Attached Files:

  10. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Oops, yes that's right.

    Wasn't it called expert in a previous version of Unity?
     
  11. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    haha... at first i thought it was too but maybe that's flash! some many flippin apps to deal with! :D
     
  12. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    "debug" was called "expert" in early versions of Unity. It was renamed to better fit the purpose of the mode. (You don't have to be expert to benefit from using the debug mode :p )