Raycast Hit Not Working (JS)

Hello,

I have a script that is supposed to be able to open an airlock door. Ideally, when you mouse over the door, a gui that was previously inactive shows and says “Open Airlock”. The problem is that the GUI Text “Open Airlock” does not become active when the raycast is pointed at the airlock. I have fiddled around with the script and came to the conclusion that the faulty part wasn’t with the raycast but was where it was saying if (hit.transform == airlockDoor). It doesn’t come up as a script error in Unity, it just doesn’t work. Please help! Here is the script:

#pragma strict

var airlockDoor : Transform;
var airlockDoorInside : Transform;
var openAirlockText : GameObject;

function Start () {

	openAirlockText.SetActiveRecursively(false);
}

function Update () {

	var hit : RaycastHit;
	var fwd = transform.TransformDirection (Vector3.forward);
	if (Input.GetKey (KeyCode.F) && Physics.Raycast (transform.position, fwd, 3) ) {
		if (hit.transform == airlockDoor) {
				airlockDoor.SendMessage("OpenAirlock", SendMessageOptions.DontRequireReceiver);
		}
			
		if (hit.transform == airlockDoorInside)
				airlockDoorInside.SendMessage("CloseAirlock", SendMessageOptions.DontRequireReceiver);
	}
	
	if (hit.transform == airlockDoor)
		openAirlockText.SetActiveRecursively(true);
		
	if ((hit.transform == airlockDoor) == false)
		openAirlockText.SetActiveRecursively(false);
}

Thanks. Any help or solution to this problem, even in C#, is very much appreciated.

There are many different “overloaded” versions of the Raycast function. You can view all of them at the scripting manual, or in MonoDevelop once you start typing out the function call.

You need to call one which populates the RaycastHit data. Perhaps something like this:

Physics.Raycast(transform.position, fwd, hit, 3)

As an aside, the raycast hit will only be populated if the raycast actually hits something (ie: the call returns “true”). It’s potentially unsafe to access the hit data if you don’t know for sure that it’s valid.

Finally, the script may act a bit unintuitively if you’ve forgotten to set airlockDoor or airlockDoorInside in the Inspector.

Problem solved, everyone! Turns out that for some reason, setting game objects active under a raycast doesn’t work, so I relayed it through a variable using the hit.transform and it worked like a charm! Thanks, Rutter!

#pragma strict

var airlockDoor : Transform;
var airlockDoorInside : Transform;
var openAirlockText : GameObject;
var mlem : boolean = false;

function Start () {

	openAirlockText.SetActiveRecursively(false);
}

function Update () {

	var hit : RaycastHit;
	var fwd = transform.TransformDirection (Vector3.forward);
	if (Physics.Raycast(transform.position, fwd, hit, 1) ) {
		mlem = true;
		print ("mlem");
		if (hit.transform == airlockDoor) {
				openAirlockText.SetActiveRecursively(true);
		}
			
		if (hit.transform == airlockDoorInside)
				airlockDoorInside.SendMessage("CloseAirlock", SendMessageOptions.DontRequireReceiver);
	}
	
	if (hit == true)
		print ("mlem");
		
	if (Physics.Raycast(transform.position, fwd, hit, 1) == false)
		mlem = false;
		
	if ((hit.transform == airlockDoor) == false)
		openAirlockText.SetActiveRecursively(false);
		
	if (mlem == true && hit.transform == airlockDoor)
		openAirlockText.SetActiveRecursively(true);
}