Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Best debugging strategy

Discussion in 'Editor & General Support' started by Entropia666, Sep 21, 2015.

  1. Entropia666

    Entropia666

    Joined:
    Sep 11, 2015
    Posts:
    6
    Hi all,
    I'm new to unity and working on a small project (which uses a lot of premade scripting).
    So basically I'm putting together packages and scripts, modifying them accordingly.

    So I deal a lot with errors of the type:

    NullReferenceException: Object reference not set to an instance of an object

    By making trials and errors I normally get to solve the problems, but it's really slow.

    What is the best strategy to debug? In MatLab I would visualize the variables step by step, I can easily debug anything. Is there any way to visualize what is contained (or NOT!) in the variable besides the Debug.Log command? Is there a way to launch the game in a step by step mode (each script, line by line!)?

    Thanks for help,
    Michelle
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,817
    Attach the mono debugger to the Unity process, hit PLAY, and the moment one of those exceptions happen, you will see exactly which line it is happening on, and the debugger will stop Unity and let you inspect all the variables in the code.

    Once you find it, if you still cannot solve it, then put additional code in your scrip to reveal useful variables in the debugger, such as saying "string foo = name;" right before the offending line, and then you can inspect the name of the GameObject to make sure it is what you expect.
     
  3. Entropia666

    Entropia666

    Joined:
    Sep 11, 2015
    Posts:
    6
    Thanks a lot!
    This really, really helped me.
    And I just solved one of the problems I was dealing with :)

    I will abuse your patience, is there a way to check where you're actually clicking? Like making the component you click/target highlighted?
    Cheers,
    Michele
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,817
    You could make a single script in your scene to take all mouse-down touches and use the Camera.main to figure out the ray that casts into the scene, and raycast into the scene to see which object it hits, then add another temporary script to that object to blink it.

    That is a tall order and makes some assumptions: the objects you want to test all have colliders and are not obscured by other colliders, including invisible ones.

    As far as "highlight," that gets into a whole other ball of rendering wax, like how to make an outline shader, which I will let you do your own googling for.

    And by temporary script, I mean one that attaches it to the game object in question, blinks itself a few times, then does a Destroy(this); to destroy the script without harming the game object or other components on the gameobject it was on.
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,852
    Use Assertions to keep an eye out for objects going null when they should not.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,817
    I decided I wanted a "TouchToWiggle" script so I wrote it.

    Is this perhaps what you are looking for? Just stick it on one object in the scene, make sure the clickable things in your scene have colliders, and start TouchingToWiggle!

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TouchToWiggle : MonoBehaviour
    6. {
    7.     public float MaxDisplacement = 0.5f;
    8.     public float VibeTime = 0.25f;
    9.  
    10.     GameObject RaycastHere( Vector3 screenPos)
    11.     {
    12.         Ray r = Camera.main.ScreenPointToRay (screenPos);
    13.         RaycastHit rch;
    14.         if (Physics.Raycast( r, out rch))
    15.         {
    16.             return rch.transform.gameObject;
    17.         }
    18.         return null;
    19.     }
    20.  
    21.     void RayCastHereAndRestoreOriginalIfAny( Vector3 screenPos, GameObject current, Vector3 origin,
    22.                                                  out GameObject newTarget, out float newTime, out Vector3 newOrigin)
    23.     {
    24.         newTime = 0;
    25.         newOrigin = Vector3.zero;
    26.         newTarget = RaycastHere (screenPos);
    27.         if (newTarget)
    28.         {
    29.             newOrigin = newTarget.transform.position;
    30.             newTime = VibeTime;
    31.             if (current)
    32.             {
    33.                 current.transform.position = origin;
    34.             }
    35.         }
    36.     }
    37.  
    38.     IEnumerator Start ()
    39.     {
    40.         GameObject vibeTarget = null;
    41.         float vibeTimer = 0.0f;
    42.         Vector3 originalPosition = Vector3.zero;
    43.  
    44.         while(true)
    45.         {
    46.             yield return null;
    47.             if (Input.GetMouseButtonDown (0))
    48.             {
    49.                 RayCastHereAndRestoreOriginalIfAny(
    50.                     Input.mousePosition, vibeTarget, originalPosition,
    51.                     out vibeTarget, out vibeTimer, out originalPosition);
    52.             }
    53.             foreach (Touch t in Input.touches)
    54.             {
    55.                 if (t.phase == TouchPhase.Began)
    56.                 {
    57.                     RayCastHereAndRestoreOriginalIfAny(
    58.                         Input.mousePosition, vibeTarget, originalPosition,
    59.                         out vibeTarget, out vibeTimer, out originalPosition);
    60.                 }
    61.             }
    62.             if (vibeTarget && vibeTimer > 0)
    63.             {
    64.                 vibeTimer -= Time.deltaTime;
    65.                 if (vibeTimer <= 0)
    66.                 {
    67.                     vibeTarget.transform.position = originalPosition;
    68.                     vibeTarget = null;
    69.                 }
    70.             }
    71.             if (vibeTarget && vibeTimer > 0)
    72.             {
    73.                 float limit = (VibeTime - vibeTimer) / VibeTime * 0.5f + 0.5f;
    74.  
    75.                 Vector3 offset = new Vector3( Random.Range ( -limit, limit),
    76.                                             Random.Range ( -limit, limit),
    77.                                             Random.Range ( -limit, limit)) * MaxDisplacement;
    78.                 vibeTarget.transform.position = originalPosition + offset;
    79.             }
    80.         }
    81.     }
    82. }
    83.  
    84.