Search Unity

Null Reference Exception in very simple script?

Discussion in 'Scripting' started by Digital-Phantom, Sep 24, 2017.

  1. Digital-Phantom

    Digital-Phantom

    Joined:
    May 31, 2013
    Posts:
    42
    I've been following a few simple tutorials so as to pick up some basics and I am now coming across this problem. Its a simple drag n drop script but I'm getting this error -

    NullReferenceException: Object reference not set to an instance of an object
    dragObject.OnMouseDrag () (at Assets/dragObject.cs:21)

    I'm probably making a real noob error, but I just can't see what it is?

    full script -

    Code (CSharp):
    1. public class dragObject : MonoBehaviour
    2. {
    3.     Vector3 dist;
    4.     float posX;
    5.     float posY;
    6.  
    7.     void OnMouseDown()
    8.     {
    9.         dist = Camera.main.WorldToScreenPoint(transform.position);
    10.         posX = Input.mousePosition.x - dist.x;
    11.         posY = Input.mousePosition.y - dist.y;
    12.     }
    13.  
    14.     void OnMouseDrag()
    15.     {
    16.         Vector3 curPos = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - posY, dist.z);
    17.         Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
    18.         transform.position = worldPos;
    19.     }
    20.  
    21. }
    Any help would be greatly appreciated, thanks !
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Is this script attached to an object that has a Collider or GUIElement?
     
  3. Digital-Phantom

    Digital-Phantom

    Joined:
    May 31, 2013
    Posts:
    42
    The object is a cube, just has the standard Box Collider on it
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    The script works fine for me. What version of Unity are you on? Could be a bug in your current Unity version. You may need to update to a new version or newer Patch Release
     
    Digital-Phantom likes this.
  5. Digital-Phantom

    Digital-Phantom

    Joined:
    May 31, 2013
    Posts:
    42
    2017, only re-installed Unity yesterday
     
  6. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Camera.main can sometimes return null.

    Code (CSharp):
    1. Camera mainCam;
    2.  
    3. void Start()
    4. {
    5.    mainCam = Camera.main;
    6.  
    7.    if(mainCam == null)
    8.       StartCoroutine(FindCamera);
    9. }
    10.  
    11. IEnumerator FindCamera()
    12. {
    13.    While(mainCam == null)
    14.    {
    15.       mainCam = Camera.main;
    16.       yield return null;
    17.    }
    18. }
     
    Digital-Phantom likes this.
  7. Digital-Phantom

    Digital-Phantom

    Joined:
    May 31, 2013
    Posts:
    42
    OK for some reason it was Unity itself. Did a un-install and fresh re-install of Unity and all is working fine. Thanks for the help guys :)
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I would avoid this.
    Just make sure the camera exists at scene startup and does not get destroyed / its tag won't be changed.