Why does Raycast2D always return non-null?

I’m trying to make it so that when you click on an object it executes a function in a script attached to that object (although at the moment I’m simply having it print a string to the console as a test). However, I noticed that no matter where I clicked on the screen it was returning a RaycastHit2D even if there wasn’t an object with a collider there. Also, when I would click on an object, the transform, collider, and rigidbody values of the returned RaycastHit2D are null.

Here’s my code (the code directly related to the mouse clicking is at the bottom):

using UnityEngine;
using System.Collections;

public class PlayerController : Ghost {

	float speedx;
	float speedy;
	bool dead = false;
	public bool faceRight = true;
	RaycastHit2D hitobject;

	void OnTriggerEnter2D(Collider2D item) {
		if (item.gameObject.tag == "possessable") {
			item.gameObject.GetComponent<PossessableObject> ().isPossessable = true;

	void OnTriggerExit2D(Collider2D item) {
		if (item.gameObject.tag == "possessable") {
			item.gameObject.GetComponent<PossessableObject> ().isPossessable = false;

	void Flip ()
		// Switch the way the player is labelled as facing.
		faceRight = !faceRight;
		// Multiply the player's x local scale by -1.
		Vector3 scale = transform.localScale;
		scale.x *= -1;
		transform.localScale = scale;

	protected override void OnUpdate() {

		speedx = rigidbody2D.velocity.x;
		speedy = rigidbody2D.velocity.y;
		anim.SetFloat ("Speedx", Mathf.Abs(speedx));
		anim.SetFloat ("Speedy", speedy);
		anim.SetBool ("Dead", dead);
		if (speedx > 0 && !faceRight) {
		else if (speedx < 0 && faceRight) {
		_motor.TargetMoveForceX = 0;
		_motor.TargetMoveForceY = 0;

		if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
			_motor.TargetMoveForceX = moveForce;
		else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
			_motor.TargetMoveForceX = -moveForce;
		if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) {
			_motor.TargetMoveForceY = moveForce;
		else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) {
			_motor.TargetMoveForceY = -moveForce;
		if (Input.GetMouseButtonDown(0)) {
			hitobject = Physics2D.Raycast(Input.mousePosition, Vector2.zero);
			if (hitobject != null) {
			//GameObject hit = hitobject.collider.gameObject;
				if (hitobject.collider == null) {
			/*if(hitobject.transform.gameObject.tag == ("possessable")) {
				//hitobject = Physics2D.Raycast(Input.mousePosition, Vector2.zero);
				//hitobject.collider.gameObject.GetComponent<PossessableObject> ().possessionEvent();
				renderer.enabled = false;

@Hoeloe is correct that RaycastHit2D is a struct so it’s a value type. From the
Physics2D.Raycast documentation:

This function returns a RaycastHit object with a reference to the collider that is hit by the ray (the collider property of the result will be NULL if nothing was hit).

(It looks like the documentation has been updated since @Benproductions1’s comment.)

Being a value type means it can never be null. If you compare it to null, you’ll get a warning like this:

warning CS0472: The result of comparing value type `UnityEngine.RaycastHit2D' with null is `true'

The RaycastHit2D documentation says to just check the result directly (which uses the “implicit conversion operator converting to bool”):

RaycastHit2D result = Physics2D.Linecast(start, end, seeTargetMask);
if (result)
    // Find something we can hurt.

I think that’s clearer than checking the collider.