GetComponent() and NullReferenceException problem

Im referencing a boolean called ray from other script on the same GameObject called Player.js. Its a codition, so Im using - if (Imput.GetButton(“Laser”) && GetComponent(Player).ray) . It works, but also give me null reference exeptions for some reason. I already used it on other scripts with no problems. What am i doing wrong?

function Update () {

	if (Input.GetButton("Laser") && GetComponent(Player).ray)
	{
		var hit0 : RaycastHit;
		var hit1 : RaycastHit;
		var hit2 : RaycastHit;
	
		if (GetComponent(Player).multiplier == 0)
		{
			if (Physics.Raycast(Vector3(transform.position.x,transform.position.y,transform.position.z + 1), Vector3.forward,hit0,100))
			{
				Instantiate (alienHit,hit0.transform.position,hit0.transform.rotation);
				DefineHitString(hit0.transform.tag);
			}
		}
		else if (GetComponent(Player).multiplier == 1)
		{
			if (Physics.Raycast(transform.position, Vector3.forward,hit0,100))
			{
				Instantiate (alienHit,hit0.transform.position,hit0.transform.rotation);
				DefineHitString(hit0.transform.tag);
			}
			
			if (Physics.Raycast(transform.position, Vector3.forward,hit1,100))
			{
				Instantiate (alienHit,hit1.transform.position,hit1.transform.rotation);
				DefineHitString(hit1.transform.tag);
			}
			
			if (Physics.Raycast(transform.position, Vector3.forward,hit2,100))
			{
				Instantiate (alienHit,hit2.transform.position,hit2.transform.rotation);
				DefineHitString(hit2.transform.tag);
			}
		}	
	}
}

I expect you have the script attached to multiple objects, and on at least one, the Player component doesn’t exist. A null reference exception error always means that the object you are attempting to reference does not exist. 100% of the time; never any other reason. So just logically think under what circumstances would the object not exist.

I think in js you need to cast the result since it returns a Component. Or store it so that the cast is done.

var script : Player = GetComponent(Player);
if(script.ray){}