[ 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
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;
}