0
\$\begingroup\$

enter image description hereenter image description hereI 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.

\$\endgroup\$
  • \$\begingroup\$ Can you show us the inspector for this object pre- and post-disappearance ? \$\endgroup\$ – DMGregory Aug 16 '19 at 10:15
  • \$\begingroup\$ @DMGregory I added the image. I had to trim them so they would be under 2MB. The top image is before hitting play and the second image is after. \$\endgroup\$ – Ethan K-B Aug 16 '19 at 11:27
2
\$\begingroup\$

As is usually the case, this is happening because you told Unity that's what you wanted to happen.

This block says:

void Start() { 
    GunAnimations = GetComponent<Animator>();

"As soon as this script starts, please throw away whatever value I'd put in the GunAnimations variable in the inspector. Instead, I want you to use the Animator component attached to the object that's running this script, if any"

But the object that's running this script ("M4A1") doesn't have an Animator component attached. That component is on a different object, one called "Shooting", according to the screenshots you showed us.

So, it looks like you either want to delete this line and stick with the value you gave the variable in the inspector, rather than overriding it at runtime, or else make sure you point your GetComponent call at the object that actually has the component you want attached to it.

\$\endgroup\$
  • \$\begingroup\$ Yeah pretty common mistake for me. Thankfully I am starting a c# college coding class in a week \$\endgroup\$ – Ethan K-B Aug 16 '19 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.