- Home /
Gun Sway Script Not Working Properly on Start
Hi,
I found this helpful gun sway script on the here that was posted as an answer, but there's one issue I have been trying to fix with it for hours. On start up I get:
NullReferenceException: Object reference not set to an instance of an object
It has to do with the function start on the script below, but it's getting the local position so I don't know why it errors. If I play past the error by unpausing it, it works perfectly. I'm very confused by this, does anyone know why?
Here's the code:
public var MoveAmount : float = 1;
public var MoveSpeed : float = 2;
public var GUN: GameObject;
public var MoveOnX : float;
public var MoveOnY : float;
public var DefaultPos : Vector3;
public var NewGunPos : Vector3;
function Start(){
DefaultPos = transform.localposition;
}
function Update () {
MoveOnX = Input.GetAxis("Mouse X") * Time.deltaTime * MoveAmount;
MoveOnY = Input.GetAxis("Mouse Y") * Time.deltaTime * MoveAmount;
NewGunPos = new Vector3 (DefaultPos.x+MoveOnX, DefaultPos.y+MoveOnY, DefaultPos.z);
GUN.transform.localPosition = Vector3.Lerp(GUN.transform.localPosition, NewGunPos,
MoveSpeed*Time.deltaTime);
}
Answer by Mortoc · Jun 10, 2012 at 01:07 AM
The only reference in your script that can be null is the GUN object. Verify that you have set the reference in the editor.
By reference do mean actually set the object to the gameobject spot in the script? Yes, this has been done. I even removed it and set this directly on the object so I wouldn't need the gameobject variable and I still got the error. Could this be a parent or child problem? What am I doing wrong? Everything should be working fine.
So I tested the gun sway script on different objects and I got the same error with every object I set it to whether it was parented or a child. No madder what I put this script to I get the error. So, there's something logically incorrect with the script.
I have exactly the same issue know this is old post but cant find solution anywhere if anyone knows please help.
Your answer
Follow this Question
Related Questions
Gun Sway Script glitching 0 Answers
Problem with quaternion weapon sway 1 Answer
Gun sways way too much 0 Answers
Aim Down Sights and Weapon Sway not working unless animations are not assigned 0 Answers
How do i make my weapon sway? 2 Answers