Raycasting Javascript (SOLVED)

[ EDIT]
The reason i go the error was due to me trying to access the collision inside of this script and not from the object i was trying to affect .

So i am making a “laser” type weapon for my top down 2.5D space adventure game.
i got everything to work but not for some reason it throws errors only when i click the left mouse button and it attempts to detect the GameObjects collision…
I know this is a simple fix but i cannot seem to figure it out even after a few hours of searching the internet.
any help is greatly appreciated :slight_smile:
here is the error
NullReferenceException: Object reference not set to an instance of an object
PlayerShooting.Fire () (at Assets/StandardGameAssets/Space Game/Scripts/Weapon Scripts/PlayerShooting.js:59)
PlayerShooting.Update () (at Assets/StandardGameAssets/Space Game/Scripts/Weapon Scripts/PlayerShooting.js:41)

and here is the script

@script RequireComponent (LineRenderer)


var line : LineRenderer;
var lineMaterial : Material;
var ray : Ray;
var hit : RaycastHit;
var range : float = 100.0;
var fireFrom : Transform;
var firePos : Vector3;
var fwd : Vector3;
var energyCurrent : int  ;
var asteroidLaserHit : boolean;
var asteroid : GameObject;



function Start () {
asteroid = GameObject.FindGameObjectWithTag("Asteroid");
line = GetComponent(LineRenderer);
line.SetVertexCount(2);
line.GetComponent(Renderer).material = lineMaterial;
line.SetWidth(0.1f, 0.25f);
firePos = fireFrom.position;





}

function Update () {

ray.origin = firePos;
if(energyCurrent <= 0) { energyCurrent = 0;}
    if(energyCurrent >= 10){
    if(Input.GetMouseButton(0)){
    Debug.Log( " my energy is = to" + energyCurrent);   
         Fire();
    
    }

}
else {
line.enabled = false;
}
if(Input.GetMouseButton(0) == false){
if(energyCurrent <= 100){
InvokeRepeating("PlusEnergy" , 0.1, 5);
Debug.Log( " my energy is = to" + energyCurrent);
}
}
}
function Fire(){

if(hit.collider.tag == ("Asteroid")){ asteroidLaserHit = true;}
   if(hit.transform.tag != "Asteroid"){ asteroidLaserHit = false;}
   if(asteroidLaserHit == true){

   hit.collider.SendMessageUpwards("TakeLaserDamage", SendMessageOptions.DontRequireReceiver);
   InvokeRepeating("MinusEnergy" , 0.1, 5);
   line.enabled = true;
   line.SetPosition(0, firePos);
   line.SetPosition(1, hit.point + hit.normal);
   }
   if(asteroidLaserHit == false) {
   return;}




      
}
function MinusEnergy () {
energyCurrent =  energyCurrent - 10;
}
function PlusEnergy () {
energyCurrent = energyCurrent + 10;
}

Does anyone know what exactly is not being referenced? im still smashing my head against the wall trying to figure out what exactly in that specific line is not being referenced.even had another buddy look at it for 30 min and couldn’t figure out what was wrong with it :frowning:

First of all, NEED HELP ASAP! in a title is really annoying when asking for help. No one likes it. And it is probably the main reason why no one have answered you so far.

As for your actual question, it looks like the answer is staring you in the face. Line 59 is identified as the problem right in your error message. So you look there, and you see (the line numbers here are off, but still) that you have many calls to hit.collider. However, you don’t seem to initialize your hit object anywhere, so hit.collider is null, and when you are trying to access its internal properties, hence the error

yeah , i changed it after 1 day when no one replied and the issue is solved, and i have no idea how to remove or mark it as solved.
But thanks for the reply