FPS 1.23 Swiching Guns

PLS HELP I GET THIS ERROR:

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)
GunScript.LateUpdate () (at Assets/Scripts/GunScript.js:166)

AND I CANT FIX IT!

my GunScript code:
var beingHeld : boolean = false;
var outsideBox : GameObject;
@HideInInspector
var countToThrow : int = -1;
@HideInInspector
var playerTransform : Transform;
@HideInInspector
var playerMovementScript : PlayerMovementScript;
var weapon : GameObject;

@HideInInspector
var camaraObject : GameObject;
@HideInInspector
var targetXRotation : float;
@HideInInspector
var targetYRotation : float;
@HideInInspector
var targetXRotationV : float;
@HideInInspector
var targetYRotationV : float;

var rotateSpeed : float = 0.3;

var holdHeight : float = -0.5;
var holdSide : float = 0.5;
var ratioHipHold : float = 1;
var hipToAimSpeed : float = 0.1;
@HideInInspector
var ratioHipHoldV : float;

var aimRatio : float = 0.4;

var zoomAngle : float = 30;

var fireSpeed : float = 15;
@HideInInspector
var waitTilNextFire : float = 0;
var bullet : GameObject;
var bulletSpawn : GameObject;

var shootAngleRandomizationAiming : float = 5;
var shootAngleRandomizationNotAiming : float = 15;

var recoilAmount : float = 0.5;
var recoilRecoverTime : float = 0.2;
@HideInInspector
var currentRecoilZPos : float;
@HideInInspector
var currentRecoilZPosV : float;

var bulletSound : GameObject;
var muzzleFlash : GameObject;

var gunbobAmountX : float = 0.5;
var gunbobAmountY : float = 0.5;
var currentGunbobX : float;
var currentGunbobY : float;

var automatic : boolean = false;

function Awake ()
{
countToThrow = -1;
playerTransform = GameObject.FindWithTag(“Player”).transform;
playerMovementScript = GameObject.FindWithTag(“Player”).GetComponent(PlayerMovementScript);
camaraObject = GameObject.FindWithTag(“MainCamera”);
}

function LateUpdate ()
{
if(beingHeld)
{
rigidbody.useGravity = false;
outsideBox.GetComponent(Collider).enabled = false;

currentGunbobX = Mathf.Sin(camaraObject.GetComponent(MouseLookScript).headbobStepCounter) * gunbobAmountX * ratioHipHold;
currentGunbobY = Mathf.Cos(camaraObject.GetComponent(MouseLookScript).headbobStepCounter * 2) * gunbobAmountY * -1 * ratioHipHold;

var holdMuzzleFlash : GameObject;
var holdSound : GameObject;

if(!automatic)
{
if(Input.GetButtonDown(“Fire1”))
{
if(waitTilNextFire <= 0)
{
if(bullet)
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
if(bulletSound)
holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
if(muzzleFlash)
holdMuzzleFlash = Instantiate(muzzleFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, ratioHipHold);
targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, ratioHipHold);
currentRecoilZPos -= recoilAmount;
waitTilNextFire = 1;
}
}
}
if(automatic)
{
if(Input.GetButton(“Fire1”))
{
if(waitTilNextFire <= 0)
{
if(bullet)
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
if(bulletSound)
holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
if(muzzleFlash)
holdMuzzleFlash = Instantiate(muzzleFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, ratioHipHold);
targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, ratioHipHold);
currentRecoilZPos -= recoilAmount;
waitTilNextFire = 1;
}
}
}

waitTilNextFire -= Time.deltaTime * fireSpeed;

if(holdSound)
holdSound.transform.parent = transform;
if(holdMuzzleFlash)
holdMuzzleFlash.transform.parent = transform;

currentRecoilZPos = Mathf.SmoothDamp(currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);

camaraObject.GetComponent(MouseLookScript).currentTargetCamaraAngle = zoomAngle;

if(Input.GetButton(“Fire2”))
{
camaraObject.GetComponent(MouseLookScript).currentAimRatio = aimRatio;
ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 0, ratioHipHoldV, hipToAimSpeed);
}

if(Input.GetButton(“Fire2”) == false)
{
camaraObject.GetComponent(MouseLookScript).currentAimRatio = 1;
ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 1, ratioHipHoldV, hipToAimSpeed);
}

transform.position = camaraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * ratioHipHold + currentGunbobX,holdHeight * ratioHipHold + currentGunbobY,0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0, 0, currentRecoilZPos));

targetXRotation = Mathf.SmoothDamp(targetXRotation,camaraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
targetYRotation = Mathf.SmoothDamp(targetYRotation,camaraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);

transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
}

if (!beingHeld)
{

rigidbody.useGravity = true;
outsideBox.GetComponent(Collider).enabled = true;

countToThrow -= 1;

if (countToThrow == 0)
rigidbody.AddRelativeForce(0, playerMovementScript.throwGunUpForce, playerMovementScript.throwGunForwardForce);

if (Vector3.Distance(weapon.transform.position, playerTransform.position) < playerMovementScript.distToPickUpGun Input.GetButtonDown(“Use Key”) playerMovementScript.waitFrameForSwitchGuns <= 0)
{
Debug.Log(“Switch Guns”);

playerMovementScript.currentGun.GetComponent(GunScript).beingHeld = false;

playerMovementScript.currentGun.GetComponent(GunScript).countToThrow = 2;

playerMovementScript.currentGun = gameObject;

beingHeld = true;

targetYRotation = camaraObject.GetComponent(MouseLookScript).yRotation - 180;

playerMovementScript.waitFrameForSwitchGuns = 2;

}

}
}

PlayerMovementScript:

var currentGun : GameObject;
var distToPickUpGun : float = 6;
var throwGunUpForce : float = 100;
var throwGunForwardForce : float = 300;
var waitFrameToSwitchGuns : int = -1;

var walkAccelaration : float = 5;
var walkDeaccelaration : float = 5;
var walkAccelAirRatio : float = 0.1;
@HideInInspector
var walkDeaccelarationVolx : float;
@HideInInspector
var walkDeaccelarationVolz : float;

var camaraObject : GameObject;
var maxWalkSpeed : float = 20;
@HideInInspector
var horizontalMovement : Vector2;

var jumpVelocity : float = 20;
@HideInInspector
var grounded : boolean = true;
var maxSlope : float = 60;

var crouchRatio : float = 0.3;
var transitionToCrouchSec : float = 0.2;
var crouchingVelocity : float;
var currentCrouchRatio : float = 1;
var originalLocalScaleY : float;
var crouchLocalScaleY : float;
var collisionDetectionSphere : GameObject;

function Awake ()
{
currentCrouchRatio = 1;
originalLocalScaleY = transform.localScale.y;
crouchLocalScaleY = transform.localScale.y * crouchRatio;
}

function LateUpdate ()
{
waitFrameToSwitchGuns -= 1;

transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentCrouchRatio);
if (Input.GetButton(“Crouch”))
currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 0, crouchingVelocity, transitionToCrouchSec);
if ( Input.GetButton(“Crouch”) == false collisionDetectionSphere.GetComponent(CollisionDetectionScript).collisionDetected == false)
currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 1, crouchingVelocity, transitionToCrouchSec);

horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if(horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement = horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
}
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.y;

if(grounded)
{
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelarationVolx, walkDeaccelaration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelarationVolz, walkDeaccelaration);
}

transform.rotation = Quaternion.Euler(0, camaraObject.GetComponent(MouseLookScript).currentYRotation, 0);

if(grounded)
rigidbody.AddRelativeForce(Input.GetAxis(“Horizontal”) * walkAccelaration * Time.deltaTime, 0, Input.GetAxis(“Vertical”) * walkAccelaration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis(“Horizontal”) * walkAccelaration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis(“Vertical”) * walkAccelaration * walkAccelAirRatio * Time.deltaTime);

if(Input.GetButtonDown(“Jump”) grounded)
rigidbody.AddForce(0, jumpVelocity, 0);

}

function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if (Vector3.Angle(contact.normal, Vector3.up)< maxSlope)
grounded = true;
}
}

function OnCollisionExit ()
{
grounded = false;
}

  1. Wrong Forum (-> Scripting)
  2. Use code tags
  3. Use private and public modifier
  4. Skip 4 if you switch to C#
  5. Use more functions to communicate between classes instead of modifing the variables of the other class ( Encapsulation (computer programming) - Wikipedia )
  6. Open Assets/Scripts/GunScript.js
    8 ) Go to line 166
  7. Search all variables used in that line
  8. Add some Debug.Log(variable) lines and fix (=assign) the variable that is causing the error!!!

haha, best reply of the day