Unity Overlap Point Causing Error
I have this code, and it's suppose to get the position of the mouse to pick up an object in a 2d space but everytime I go to pick up an object I get an error at the Physics2D.OverlapPoint. The error is a null reference exception saying the object reference is not set to an instance of an object. I tried Google but couldn't find anything. Thank you.
 private bool simStarted = false;
 [SerializeField] private GameObject grabbedObject = null;
 [SerializeField] private Camera cam;
 private void Update()
 {
     if(!simStarted)
     {
         if(Input.GetKeyDown(KeyCode.Mouse0) && grabbedObject == null)
         {
             Vector2 mousePos = Input.mousePosition;
             Vector2 mouseWorldPos = cam.ScreenToWorldPoint(mousePos);
             Debug.Log(mouseWorldPos);
          //ERROR   if(Physics2D.OverlapPoint(mouseWorldPos, 8).gameObject != null)
             {
                 grabbedObject = Physics2D.OverlapPoint(mouseWorldPos, 8).gameObject;
             }
             Debug.Log("Nothing");
         }
         else if(Input.GetKeyDown(KeyCode.Mouse0) && grabbedObject != null)
         {
             grabbedObject.transform.position = new Vector3(Mathf.Round(transform.position.x), Mathf.Round(transform.position.y), 0);
             grabbedObject = null;
         }
         if(grabbedObject != null)
         {
             grabbedObject.transform.position = Input.mousePosition;
         }
     }
 }
}
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
Problems with raycasting 2D. Probelmas com raycasting 2D. 0 Answers
How do I create a 2D movement script that will cause prefabs to drift around the screen? 0 Answers
Random position, overlap problem. 1 Answer
Forcibly halt movement of slow rigidbodies, but not falling ones. (2D) 1 Answer
Player Dips Into Ground After Jump 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                