This Wont Work Whe i switch guns i get the 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.Update () (at Assets/GunScript.js:125)
BTW LINE 125 and its 136 lines
var playerMovementScript : PlayerMovementScript;
var cameraObject : 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 racioHipHold : float = 1;
var hipToAimSpeed : float = 0.1;
@HideInInspector
var racioHipHoldV : float;
var aimRacio : float = 0.2;
var zoomAngle : float = 5;
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 muzzelFlash : GameObject;
var gunbobAmountX : float = 0.5;
var gunbobAmountY : float = 0.5;
var currentGunbobX : float;
var currentGunbobY : float;
function Awake ()
{
countToThrow = -1;
playerTransform = GameObject.FindWithTag("Player").transform;
playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
cameraObject = GameObject.FindWithTag("MainCamera");
}
function Update ()
{
if (beingHeld)
{
rigidbody.useGravity = false;
outsideBox.GetComponent(Collider).enabled = false;
currentGunbobX = Mathf.Sin(cameraObject.GetComponent(MouseLookScript).headbobStepCounter) * gunbobAmountX * racioHipHold;
currentGunbobY = Mathf.Cos(cameraObject.GetComponent(MouseLookScript).headbobStepCounter * 2) * gunbobAmountY * -1 * racioHipHold;
var holdMuzzelFlash : GameObject;
var holdSound : GameObject;
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 (muzzelFlash)
holdMuzzelFlash = Instantiate(muzzelFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
currentRecoilZPos -= recoilAmount;
waitTilNextFire = 1;
}
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
if (holdSound)
holdSound.transform.parent = transform;
if (holdMuzzelFlash)
holdMuzzelFlash.transform.parent = transform;
currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle = zoomAngle;
if (Input.GetButton("Fire2")){
cameraObject.GetComponent(MouseLookScript).currentAimRacio = aimRacio;
racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);}
if (Input.GetButton("Fire2") == false){
cameraObject.GetComponent(MouseLookScript).currentAimRacio = 1;
racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);}
transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold + currentGunbobX, holdHeight * racioHipHold + currentGunbobY, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0,0,currentRecoilZPos));
targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.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(transform.position, playerTransform.position) < playerMovementScript.distToPickUpGun && Input.GetButtonDown("Use Key") && playerMovementScript.waitFrameForSwitchGuns <= 0)
{
playerMovementScript.currentGun.GetComponent(GunScript).beingHeld = false;
playerMovementScript.currentGun.GetComponent(GunScript).countToThrow = 2;
playerMovementScript.currentGun = gameObject;
beingHeld = true;
targetYRotation = cameraObject.GetComponent(MouseLookScript).yRotation - 180;
playerMovementScript.waitFrameForSwitchGuns = 2;
}
}
}