- Home /
Null Ref Exeption
Hi, I started a project then this error came from nowhere, please help me :( this is my code:
private Transform firstPerson_View;
private Transform firstPerson_Camera;
private Vector3 firstPerson_View_Rotation = Vector3.zero;
private float speed;
private bool is_Moving, is_Grounded, is_Crouching;
private float inputX, inputY;
private float inputX_Set, inputY_Set;
private float inputModifyFactor;
private float antiBumpFactor = 0.75f;
private bool limitDiagonalSpeed;
private CharacterController charController;
private Vector3 moveDirection = Vector3.zero;
public float walkSpeed = 6.75f;
public float runSpeed = 10f;
public float crouchSpeed = 4f;
public float jumpSpeed = 8f;
public float gravity = 20f;
void Start ()
{
firstPerson_View = transform.Find("FPS View");
charController = GetComponent<CharacterController>();
speed = walkSpeed;
is_Moving = false;
}
void Update ()
{
PlayerMovement();
}
void PlayerMovement()
{
if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
{
if(Input.GetKey(KeyCode.W)){inputY_Set = 1f;}
else{inputY_Set = -1f;}
}
else{inputY_Set = 0f;}
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
if(Input.GetKey(KeyCode.A)){inputX_Set = -1f;}
else{inputX_Set = 1f;}
}
else{inputX_Set = 0f;}
inputY = Mathf.Lerp(inputY, inputY_Set, Time.deltaTime * 19f);
inputX = Mathf.Lerp(inputX, inputX_Set, Time.deltaTime * 19f);
inputModifyFactor = Mathf.Lerp(inputModifyFactor, (inputY_Set != 0 && inputX_Set != 0 && limitDiagonalSpeed) ? 0.75f : 1.0f , Time.deltaTime * 19f);
firstPerson_View_Rotation = Vector3.Lerp(firstPerson_View_Rotation, Vector3.zero, Time.deltaTime * 5f);
firstPerson_View.localEulerAngles = firstPerson_View_Rotation;
if(is_Grounded)
{
moveDirection = new Vector3(inputX * inputModifyFactor, -antiBumpFactor, inputY * inputModifyFactor);
moveDirection = transform.TransformDirection(moveDirection) * speed;
}
moveDirection.y -= gravity * Time.deltaTime;
is_Grounded = (charController.Move(moveDirection * Time.deltaTime) & CollisionFlags.Below) != 0;
is_Moving = charController.velocity.magnitude > 0.15f;
}
}
the error code line is: firstPerson_View.localEulerAngles = firstPerson_View_Rotation;
Answer by Chyakka · Feb 17, 2020 at 05:03 PM
The null reference exception error occurs when a referenced variable is null in your case it's your
firstPerson_View_Rotation
variable.
The cause of this error would be
firstPerson_View = transform.Find("FPS View");
Double check the transform that has the script attached and ensure there is a child named FPS View.
Your answer
Follow this Question
Related Questions
NullReference 2 Answers
NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start () 2 Answers
NullReferenceException: Object reference not set to an instance of an object... 0 Answers
NullReferenceException with UNET void 0 Answers
[Mirror Networking] NullReferenceException on player when trying to start server a second time 1 Answer