I check if an object is not null, yet inside the same if statement I get a null reference exception from the very same object. Why is this happening?

So I’m having an issue with a null reference exception, whereby I want to run a public method from another class using OnTriggerEnter. To make sure that the collider that enters in question has that class as a component I use the following:

public class WaterDetector : MonoBehaviour {

	WaterSource waterSource;

	void Start()
		waterSource = transform.parent.GetComponent<WaterSource> ();

	void OnTriggerEnter2D(Collider2D hit)
		if (hit.gameObject.GetComponent<Player>() != null) {
			**waterSource.Splash(transform.position.x, hit.gameObject.GetComponent<Player>().GetYVelocity());**


The reason I am so confused is that according to the console, the line I get the exception from is the one inside the if statement (surrounded by **), which has just checked to make sure the Player component exists, so that line shouldn’t run if the Player component is null (right?)

I also know that the WaterSource component is not null because I previously had a test object that was able to call the Splash function with no issues at all, which used a different component’s method to provide the second parameter to the Splash function. (also, I believe if this was the origin then the exception would occur at the line in the start method where waterSource is set)

Finally, I know that the GetYVelocity method doesn’t return null since as a debugging attempt, I set the function to always return 5.

I am led to the conclusion that somehow the if statement condition returns true, but in the next line the reverse is true and I get the exception. Is my understanding of this wrong somehow?

Thanks in advance for your help.

As it happens, I had previously changed way waterSource was parented in the hierarchy and the object this script was on was a ‘grandchild’ of the target (I don’t know if people actually word it like that or not) so I just had to add an extra ‘.parent’ in the Start method. As this change was made AFTER I tested the waterSource object and I didn’t realise this, I was quite certain that that wasn’t the problem.

Thanks to @mj321 and @NoseKills for the helpful debugging info!