hit.collider with raycast not working

What I’m, trying to do:
When my player is looking at the doors, he can left click and make the door’s script play the animation to open / close.

My player camera’s script:

var Door : Door_anim;


function FixedUpdate()
{
    var hit : RaycastHit;
    var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    //if(Physics.Raycast(ray, dist))
    //{
    //print("Found an object - distance: " + hit.distance);
    //}
    if(Physics.Raycast(ray, dist) && hit.collider.tag == "Doors" && Input.GetMouseButton(0))
    {
    Door = hit.collider.gameObject.GetComponent("Door_anim");
    Door.animate();
    }
}

The door’s script
```javascript
**#pragma strict

var triggered = false;
var closed = true;

function animate()
{
if(closed)
{
GetComponent.().Play(“Door_open”);
closed = false;
}
if(!closed)
{
GetComponent.().Play(“Door_close”);
closed = true;
}
}**
```
The door


The error i get when looking at ANY object with a hitbox

Thanks in advance master scripters :smile:

You need to pass your RaycastHit reference into the Physics.Raycast method to get any data out of it. As you have written it, “hit” is still null when you start using it. Reread Unity’s sample code for the exact syntax and parameter order in JS.

Also obligatory: JS is dead, and depending on how deep into the project you are, this would be a good time to learn C# and convert your project to it.

1 Like

Ah thanks!
I placed the RaycastHit reference into Physics.Raycast and now it works.

Also, since when is JS dead and why?

https://blogs.unity3d.com/2017/08/11/unityscripts-long-ride-off-into-the-sunset/