How to ignore not having the requested information in raycasthit

I have a function which utilizes a variable found within the class TakeDamage (which is found on another gameObject…gameObject is another game object whereas GameObject is the game object the script is attached to correct?):

void SeeHealth ()
		{
				Ray displayHealth = Camera.main.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
				if (Physics.Raycast (displayHealth, out hitInfo, 500)) {
						float targetHealth = hitInfo.rigidbody.gameObject.GetComponent<TakeDamage> ().Health;
				}	
		}

Which operates exactly how I want it to until it hits a gameObject that doesn’t have the class TakeDamage at which point it throws a NullReferenceException.

If I look into the sky (no sky box) nothing happens, which is the desired outcome for what I want to have happen when I look at a gameObject without the class TakeDamage.

I know I can “ignore” information I send with a raycast with hitInfo.transform.SendMessage("function", component,SendMessageOptions.DontRequireReceiver) if there is no receiving component found on the target hit by the ray. Is there a similar way to not require a “transmitting” component on the target?

Hopefully I’m explaining my desired outcome correctly. I’m probably over thinking this as I have little knowledge in c#. Any suggestions are welcome, though I’d prefer one that doesn’t require rewriting more than this function. The code above has been stripped of everything that I thought wasn’t relevant to the question, if more information is requested I can attempt to provide it.

Thank you for any and all help!

void SeeHealth ()
{
Ray displayHealth = Camera.main.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast (displayHealth, out hitInfo, 500)) {
TakeDamage takeDamage = hitInfo.rigidbody.gameObject.GetComponent ();
if (takeDamage){
targetHealth = takeDamage.Health;
}
}
}

Thank you very much but I’ve figured out my answer. All I needed to do was throw in an &&(hitInfo.transform.tag == “x”) in the if statement so

if (Physics.Raycast (displayHealth, out hitInfo, 500)) {

becomes my current method for creating the desired outcome

if (Physics.Raycast (displayHealth, out hitInfo, 500)&&(hitInfo.transform.tag == "Enemy" || hitInfo.transform.tag == "Player")) {