FPS 1.23 Swiching Guns (508838)

Hey Guys,
In 1.23 i always get an error when im presing the “E”-key and the guns dont switch.
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:164)

Here is my GunScript Code :

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

@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(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 = camaraObject.GetComponent(MouseLookScript).yRotation - 180;

playerMovementScript.waitFrameForSwitchGuns = 2;

}

}
}

thats one hell of a script. put code tags when you paste the script in the thread so it can be easier to understand.

anyways have you tried to remove the weapon switching code out of the script and than putting it in another script. perhaps a weapon switching script.

if you dont know what i mean, i provided steps to simplify what i mean ( LOL )

- 1 - remove the weapon switching code from the script.

  • 2 - Create a new javascript.
  • 3 - Write one of the following code into your new script that you made (weapon switching script) ------
#pragma strict

//You can write one of the following Varibles in here like
var Pistol : GameObject;

function Start () {

    Pistol.active = false;    

}
//you dont have to put the     Pistol.active = false;  line



//function update happens EverySingleFrame
function Update () {
    //If we hit E what do we want it to do
    if(Input.GetKeyDown(KeyCode.E){
    SwitchWeapons();
    //You Dont have to declare the function but i like to because when you declare a function you can write more code in it.
    }
}


//the switch weapons Function (the one we declared in the update function
function SwitchWeapons () {

    Pistol.active = true;
    
}

//dont worry about the one line code in the function, you can toggle it every time
  • 4 - Then save the script, and than make a new game object and put the weapons script in the game object. (it will act like a manager)
  • 5 - Then put one of the following code in the weapon script
var IsDrawed = false;
var DrawTime : float = 1;


function Update () {
//include this code in the update function
	if(Input.GetKeyDown(KeyCode.E)){
	DrawedWeapon();
	}
}

function DrawedWeapon () {

	animation.Play("DrawWeapon");
	audio.PlayOneShot(DrawSound);

	yield WaitForSeconds (DrawTime);

}

you don’t have to put the code in it exactly its basically an idea

so im not sure if this dose not work for you cause i haven’t tested it yet.

but you get the general idea

@Frostbite. Unfortunately, that’s not how the script works. It picks up the object on the ground, and drop yours (same object) and everything stays the same (ammo, etc).

Anyways, post with code tags, and we can be a little more help. As it is now, I’m not counting out 164 lines to find the error. :slight_smile:

oh, well all i did though was a weapon switching script rather than weapon pickup script.

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

@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(Play erMovementScript);
camaraObject = GameObject.FindWithTag("MainCamera"); 
}


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

currentGunbobX = Mathf.Sin(camaraObject.GetComponent(MouseLookScrip t).headbobStepCounter) * gunbobAmountX * ratioHipHold;
currentGunbobY = Mathf.Cos(camaraObject.GetComponent(MouseLookScrip t).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).current TargetCamaraAngle = zoomAngle;

if(Input.GetButton("Fire2"))
{
camaraObject.GetComponent(MouseLookScript).current AimRatio = aimRatio;
ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 0, ratioHipHoldV, hipToAimSpeed);	
}

if(Input.GetButton("Fire2") == false)
{
camaraObject.GetComponent(MouseLookScript).current AimRatio = 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.GetC omponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
targetYRotation = Mathf.SmoothDamp(targetYRotation,camaraObject.GetC omponent(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(GunSc ript).beingHeld = false;

playerMovementScript.currentGun.GetComponent(GunSc ript).countToThrow = 2;

playerMovementScript.currentGun = gameObject;

beingHeld = true;

targetYRotation = camaraObject.GetComponent(MouseLookScript).yRotati on - 180;

playerMovementScript.waitFrameForSwitchGuns = 2;

}


}
}

Been a few days so not sure if he even still needs help, but thought I’d wrap his code in tags for others to look at better.

Looks like if (Vector3.Distance(transform.position, playerTransform.position) < playerMovementScript.distToPickUpGun Input.GetButtonDown("Use Key") playerMovementScript.waitFrameForSwitchGuns <= 0)

I’m a complete newbie at programming. Trying to teach myself so guess if I’m wrong, there will be a mistake to learn from xD

Not sure if it’s the correct line, but with that error, wouldn’t it be Input.GetButtonDown("Use Key") at fault? E.g. Is there a button called, “Use Key” and does it pass a non-null value? I say this, because surely it must check the other values whenever it updates which would flag an error if they did, and it happens exactly when you press the E-key which must make it return a null when pressed? Might limit your search to the “Use Key” code if it’s right.

Like I said, I’m a complete newbie. Just thought I’d try to help xD Interested to see some people with actual knowledge solve it :slight_smile: