- Home /
Why won't this weapon sway script work?
I'm trying to get this script to work, but it says "NullReferenceException: Object reference not set to the instance of an object" On line 16. Please help, and thanks ahead of time!
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);
}
From counting down line 16 is DefaultPos, but that doesn't seem right. Which one is it? Also, it is convention that all declared variables should start lowercase then every other word after be upper case, I don't remember the na$$anonymous$$g standard.
It is the line: DefaultPos = transform.localposition;
But I don't know what's wrong with it, I'm new to this, can anyone help?
lol, uppercase P. DefaultPos = transform.localPosition; These bite me in the ass all the time. Always make sure to refer to the documentation to double check syntax and definition.
Thanks! Well, it got rid of the error, but the script still isn't making any effect at all... Any ideas? I've been so frustrated over this script, I'm so lost...
Answer by robertbu · Jan 24, 2013 at 05:59 AM
One of your problems is how you are using Lerp(). The final parameter of Lerp() uses a value between 0 and 1. Larger values are truncated so only the fractional part is used. For most uses, this value needs to increase or decrease over time. Time.deltaTime, since it is the time since the last frame, will not steadily increase or decrease.Change Time.deltaTime to Time.time;
Your answer