Search Unity

Pause code based on events?

Discussion in 'Scripting' started by GoodNight9, Nov 22, 2015.

  1. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Hello Everyone again!

    This time I have a more general question. Is there any way to (while having pressed the play button) have the game pause and let you see exactly what line of code it was running right before pausing?

    I have a turret that aims at a GameObject target. And every other frame the turret believes the target is null. When playing the game, it's almost impossible to notice. However, the target isnt' supposed to be null unless the target disappears (which it isn't).

    I was wondering if there was a way to tell the computer "If the target becomes null, STOP and tell me the last line of code you ran."

    Thank you again for your time-
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The simplest way is to turn on error pause and put in a Debug.LogError statement.

    For more versatility you can attach a debugger externally and set up break points.
     
  3. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Thank you. But does this work for things that aren't errors as well? My game doesn't return an error when the object is null. The only way I figured that out was by having it print it's status in the update method.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Throw your own error for debugging purposes.
     
  5. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Ah thank you very much for that :)
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    In that specific question, you won't be able to ever see the LAST line of code that was run. However, you could rewrite 'target' as a property, and intercept it AS it is being changed:
    Code (csharp):
    1. public Transform target { //I'm guessing it's a transform, but it can be whatever
    2. set {
    3. if (value != _target) {
    4. Debug.Log("Changing target to "+(value != null ? value.gameObject.name : "NULL") );
    5. _target = value;
    6. }
    7. get {
    8. return _target;
    9. }
    10. }
    11. private Transform _target;
    Now, anytime this thing is changed, it will tell you so in the console; if you click on this in the console, you can then dig into the stacktrace and see exactly where this change was made.

    (Note: If the previous target's GameObject gets destroyed, the value will be, functionally, NULL, but that property will not have been called to change it. If that's what's happening, this won't help you.)
     
  7. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    That is a beautiful idea. I like that a lot. I was able to eventually find what was going on. Thank you!