I am working on a gun script and it worked fine until earlier today when I made a change to the script and it stopped working. I put it back, but for some reason now the animator object attached to the script keeps disappearing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RifleShooting : MonoBehaviour
{
private float NextTimeToFire = 0f;
private int CurrentAmmo;
private bool IsReloading = false;
[Space(10)]
[Header("Floats")]
public float Damage = 10.0f;
public float Range = 100.0f;
public float ImpactForce = 60f;
public float FireRate = 15f;
public float ReloadTime = 1.0f;
[Space(10)]
[Header("Others")]
[Space(5)]
[Space(10)]
[Header("Others")]
[Space(5)]
public int MaxAmmo = 10;
public Camera FPSCamera;
public Animator GunAnimations;
public ParticleSystem MuzzleFlash;
public GameObject impactEffect;
public bool AllowedToShoot = true;
// Start is called before the first frame update
void Start()
{
GunAnimations = GetComponent<Animator>();
if (CurrentAmmo == -1)
CurrentAmmo = MaxAmmo;
}
private void OnEnable()
{
IsReloading = false;
GunAnimations.SetBool("Reloading", false);
}
// Update is called once per frame
void Update()
{
if (IsReloading)
return;
if (CurrentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (AllowedToShoot == false) return;
{
if (Input.GetButton("Fire1") || Input.GetButtonDown("Fire1") && Time.time >= NextTimeToFire)
{
NextTimeToFire = Time.time + 1f / FireRate;
Shoot();
}
}
}
IEnumerator Reload()
{
IsReloading = true;
Debug.Log("Reloading...");
GunAnimations.SetBool("Reloading", true);
GunAnimations.SetBool("IsShooting", false);
yield return new WaitForSeconds(ReloadTime - .25f);
GunAnimations.SetBool("Reloading", false);
yield return new WaitForSeconds(.25f);
GunAnimations.SetBool("IsShooting", true);
CurrentAmmo = MaxAmmo;
IsReloading = false;
}
void Shoot()
{
//shooting script here
}
}
Also if it helps here is what I see in the console when I try to fire:
MissingComponentException: There is no 'Animator' attached to the "M4A1" game object, but a script is trying to access it.