Preventing a player firing unequipped

Hope you can help,

I’m unable to get the player to stop firing when unequipped. So far the only method that I can get to work is using the players sword collider to disable the fire method, which is not ideal for more than 2 weapons.

using System.Collections;
using System.Collections.Generic;//
using UnityEngine;//

public class Loadedweapon : MonoBehaviour//
{
    public GameObject Machinegunloaded;//
    public GameObject Spawngunloaded;
    public GameObject Spawngunempty;
    public GameObject Crosshair;
    public GameObject Machinegunspawn;
    public GameObject Gunspawn;

    bool Isfiring;
    private void Update()
    {
        if (!Isfiring)
    {
        Spawngunloaded.SetActive(false);
        Machinegunloaded.SetActive(false);
        Spawngunempty.SetActive(true);//sword
        Crosshair.SetActive(false);
    }
        }
    void OnTriggerEnter(Collider other)
    // works attached to player
    {
        //equip tag for rifle is equip, for sword swordequip
        if (other.tag == "swordequip" && Isfiring)// trigger to detect weapon not player , so to determine isequipped to prevent fire when unequipped
        {
            Spawngunloaded.SetActive(false);
            Machinegunloaded.SetActive(false);
            Spawngunempty.SetActive(true);//sword
            Crosshair.SetActive(false);
            gameObject.GetComponent<MeshRenderMelee>().enabled = true;//
        }
        // using unique tags seems to work best
        //equip tag for rifle is equip, for sword swordequip
        if (other.tag == "rifleequip" && Isfiring)// trigger to detect weapon not player, so to determine isequipped to prevent fire when unequipped

        {
            Spawngunloaded.SetActive(true);
            Machinegunloaded.SetActive(false);
            Spawngunempty.SetActive(false);//sword no bullets so set empty
            Crosshair.SetActive(true);
            Machinegunspawn.SetActive(false);
            Gunspawn.SetActive(true);

        }
        if (other.tag == "machinegunequip" && Isfiring)// trigger tag method to detect weapon , so to determine isequipped to prevent fire when unequipped

        {
            Machinegunloaded.SetActive(true);
            Spawngunloaded.SetActive(false);
            Spawngunempty.SetActive(false);
            Crosshair.SetActive(true);//
            Machinegunspawn.SetActive(true);
            Machinegunspawn.SetActive(true);
            Gunspawn.SetActive(false);
        }

        if (Input.GetAxisRaw("Fire1") != 0)// right trigger of a game controller
        {
            Isfiring = true;
        }

        else if (other.tag != "machinegunequip" && Isfiring) // if player is not making contact with           item in hand the following should heappen  (does not work)
        {
            Spawngunloaded.SetActive(false);
            Machinegunloaded.SetActive(false);
            Spawngunempty.SetActive(true);// empty gun load out usually works
            Crosshair.SetActive(false);
        }
        else if (other.tag != "rifleequip" && Isfiring)
        {
            Spawngunloaded.SetActive(false);
            Machinegunloaded.SetActive(false);
            Spawngunempty.SetActive(true);// empty gun load out usually works
            Crosshair.SetActive(false);
        }
}

}

}

Steps to success:

  • identify / create a boolean expression that captures “do I have a sword equipped?”

  • use that value to decide if you allow or inhibit an attack intention.

Thanks been trying that a fair while and became content just with my simple method of using the sword to disable. Be good to resolve but has taken far too much time for something so trivial. Maybe I will return to it with more speedy resolution knowledge.