NullReferenceException: Object reference not set to an instance of an object
HandGun.playerMove () (at Assets/Scripts/Rifles/HandGun.cs:152)
HandGun.Update () (at Assets/Scripts/Rifles/HandGun.cs:92)
here is the code
private void Update()
{
if(HandgunActive == true)
{
animator.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("GunAnimator");
}
OnSurface = Physics.CheckSphere(surfaceCheck.position, surfaceDistance, surfaceMask);
if (OnSurface && velocity.y < 0)
{
velocity.y = -2f;
}
//gravity
velocity.y += gravity * Time.deltaTime;
cC.Move(velocity * Time.deltaTime);
playerMove();
Jump();
Sprint();
if (setReloading)
return;
if (presentAmmunition <= 0)
{
StartCoroutine(Reload());
return;
}
if(isMoving == false)
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToShoot)
{
animator.SetBool("Shoot", true);
nextTimeToShoot = Time.time + 1f / fireCharge;
Shoot();
}
else
{
animator.SetBool("Shoot", false);
}
}
}
void playerMove()
{
float horizontal_axis = Input.GetAxisRaw("Horizontal");
float vertical_axis = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal_axis, 0f, vertical_axis).normalized;
if (direction.magnitude >= 0.1f)
{
animator.SetBool("WalkForward", true);
animator.SetBool("RunForward", false);
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + playerCamera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(PlayerTransform.eulerAngles.y, targetAngle, ref turnCalmVelocity, turnCalmTime);
PlayerTransform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
cC.Move(moveDirection.normalized * playerSpeed * Time.deltaTime);
jumpRange = 0f;
isMoving = true;
handgun2.isMoving = true;
}
else
{
animator.SetBool("WalkForward", false);
animator.SetBool("RunForward", false);
jumpRange = 1f;
isMoving = false;
handgun2.isMoving = false;
}
}
void Jump()
{
if (Input.GetButtonDown("Jump") && OnSurface)
{
animator.SetBool("IdleAim", false);
animator.SetTrigger("Jump");
velocity.y = Mathf.Sqrt(jumpRange * -2 * gravity);
}
else
{
animator.SetBool("IdleAim", true);
animator.SetTrigger("Jump");
}
}
void Sprint()
{
if (Input.GetButton("Sprint") && Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow) && OnSurface)
{
float horizontal_axis = Input.GetAxisRaw("Horizontal");
float vertical_axis = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal_axis, 0f, vertical_axis).normalized;
if (direction.magnitude >= 0.1f)
{
animator.SetBool("WalkForward", false);
animator.SetBool("RunForward", true);
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + playerCamera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(PlayerTransform.eulerAngles.y, targetAngle, ref turnCalmVelocity, turnCalmTime);
PlayerTransform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
cC.Move(moveDirection.normalized * playerSprint * Time.deltaTime);
jumpRange = 0f;
isMoving = true;
handgun2.isMoving = true;
}
else
{
animator.SetBool("WalkForward", true);
animator.SetBool("RunForward", false);
jumpRange = 1f;
isMoving = false;
handgun2.isMoving = false;
}
}
}
void Shoot()
{
if (mag == 0)
{
//show ammo out text/ui
StartCoroutine(ShowAmmoOut());
return;
}
{
}
presentAmmunition--;
if (presentAmmunition == 0)
{
mag--;
}
muzzleSpark.Play();
RaycastHit hitInfo;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hitInfo, shootingRange))
{
Debug.Log(hitInfo.transform.name);
Object obj = hitInfo.transform.GetComponent<Object>();
if (obj != null)
{
obj.objectHitDamage(giveDamage);
GameObject metalEffectGo = Instantiate(metalEffect, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
Destroy(metalEffectGo, 1f);
}
}
}