I have 2 scripts one is to shoot and one is to pick up guns but when I drop the guns I can still shoot

I have 2 scripts one is to shoot and one is to pick up guns but when I drop the guns I can still shoot. How do I make it not shoot when I drop it? I copied a tutorial online but I see one of the last lines is disable gunscript. I’m not sure why its not working.

GunSystem.cs

<using UnityEngine;
using TMPro;

public class GunSystem : MonoBehaviour
{
//Gun stats
public int damage;
public float timeBetweenShooting, spread, range, reloadTime, timeBetweenShots;
public int magazineSize, bulletsPerTap;
public bool allowButtonHold;
int bulletsLeft, bulletsShot;

//bools 
bool shooting, readyToShoot, reloading;


//Reference
public Camera fpsCam;
public Transform attackPoint;
public RaycastHit rayHit;
public LayerMask whatIsEnemy;


//Graphics
public GameObject muzzleFlash, bulletHoleGraphic;
public float camShakeMagnitude, camShakeDuration;
public TextMeshProUGUI text;


private void Awake()
{
    bulletsLeft = magazineSize;
    readyToShoot = true;
}
private void Update()
{
    MyInput();


    //SetText
    text.SetText(bulletsLeft + " / " + magazineSize);
}
private void MyInput()
{
    if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
    else shooting = Input.GetKeyDown(KeyCode.Mouse0);


    if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();


    //Shoot
    if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
    {
        bulletsShot = bulletsPerTap;
        Shoot();
    }
}
private void Shoot()
{
    readyToShoot = false;


    //Spread
    float x = Random.Range(-spread, spread);
    float y = Random.Range(-spread, spread);


    //Calculate Direction with Spread
    Vector3 direction = fpsCam.transform.forward + new Vector3(x, y, 0);


    //RayCast
    GameObject flash = Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity, attackPoint);
    Destroy(flash, 0.1f);

    if (Physics.Raycast(fpsCam.transform.position, direction, out rayHit, range))
    {
        if (((1 << rayHit.collider.gameObject.layer) & whatIsEnemy) != 0)
        {
            Debug.Log(rayHit.collider.name);

            // if (rayHit.collider.CompareTag("Enemy"))
            // rayHit.collider.GetComponent<ShootingAi>().TakeDamage(damage);
        }
        else
        {
            Instantiate(bulletHoleGraphic, rayHit.point, Quaternion.LookRotation(rayHit.normal));
        }
    }


    bulletsLeft--;
    bulletsShot--;


    Invoke("ResetShot", timeBetweenShooting);


    if (bulletsShot > 0 && bulletsLeft > 0)
        Invoke("Shoot", timeBetweenShots);
}
private void ResetShot()
{
    readyToShoot = true;
}
private void Reload()
{
    reloading = true;
    Invoke("ReloadFinished", reloadTime);
}
private void ReloadFinished()
{
    bulletsLeft = magazineSize;
    reloading = false;
}

}>

PickUpController.cs

<using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PickUpController : MonoBehaviour
{
public GunSystem gunScript;
public Rigidbody rb;
public BoxCollider coll;
public Transform player, gunContainer, fpsCam;

public float pickUpRange;
public float dropForwardForce, dropUpwardForce;

public bool equipped;
public static bool slotFull;

private void Start()
{
    //Setup
    if (!equipped)
    {
        gunScript.enabled = false;
        rb.isKinematic = false;
        coll.isTrigger = false;
    }
    if (equipped)
    {
        gunScript.enabled = true;
        rb.isKinematic = true;
        coll.isTrigger = true;
        slotFull = true;
    }
}

private void Update()
{
    
    //Check if player is in range and "E" is pressed
    Vector3 distanceToPlayer = player.position - transform.position;
    if (!equipped && distanceToPlayer.magnitude <= pickUpRange && Input.GetKeyDown(KeyCode.E) && !slotFull) PickUp();

    //Drop if equipped and "Q" is pressed
    if (equipped && Input.GetKeyDown(KeyCode.Q)) Drop();
}

private void PickUp()
{
    equipped = true;
    slotFull = true;

    //Make weapon a child of the camera and move it to default position
    transform.SetParent(gunContainer);
    transform.localPosition = Vector3.zero;
    transform.localRotation = Quaternion.Euler(Vector3.zero);
    transform.localScale = Vector3.one;

    //Make Rigidbody kinematic and BoxCollider a trigger
    rb.isKinematic = true;
    coll.isTrigger = true;

    //Enable script
    gunScript.enabled = true;
}

private void Drop()
{
    equipped = false;
    slotFull = false;

    //Set parent to null
    transform.SetParent(null);

    //Make Rigidbody not kinematic and BoxCollider normal
    rb.isKinematic = false;
    coll.isTrigger = false;

    //Gun carries momentum of player
    rb.velocity = player.GetComponent<Rigidbody>().velocity;

    //AddForce
    rb.AddForce(fpsCam.forward * dropForwardForce, ForceMode.Impulse);
    rb.AddForce(fpsCam.up * dropUpwardForce, ForceMode.Impulse);
    //Add random rotation
    float random = Random.Range(-1f, 1f);
    rb.AddTorque(new Vector3(random, random, random) * 10);

    //Disable script
    gunScript.enabled = false;
}

}>

TBH it is hard to read like that, but, it sounds like debugging is needed to see what values are setting and unsetting and why its not working

I tried using the brackets but they did not work as you can see lol

The GunSystem uses Invoke which still runs when the script is disabled. Try adding this:

    void OnDisable()
    {
        CancelInvoke();
    }

Where in the script would I put that? I get a green squiggly line under the OnDisable unless i put it at the bottom. I tried in a few spots.

Question: Do you understand the code structure you have?

nah i dont im just trying to copy a bunch of tutorials systems than make a game like that. So I have like placeholder scripts I can use when I’m learning how to do specific things. Do you know where to place the invoke thing? So I can troubleshoot it another way if I need.

OK, didnt think so - best advice? go follow a tutoirial that explains everything properly. However, as a quick (I have a migraine and so it is very limited)

So you showed 2 classes, lets look at the pickup controller

OK, so the code you showed us is a class, (hence it has public class ) everything grouped in c# has {} round it, so code chunks, methods, classes etc.

if you think of classes like templates for objects such as a cup, you can fill the cup, see how full the cup is, drink the cup, empty the cup, wash the cup… and of course, you can have many cups, they all work the same, but, they all are instances of cup

insidde you then had a number of variables, for some reasom all yours are public, as a rule, public ones only need to be public if other scripts MUST access them, normally this is not generally true.

You also have a static variable, this means the many copies you might have would share the variable, so if one instance says your slotFull, ALL of them would… it is not a per instance variable, like the rest. This is also generally not normal, there are valid reasons, but slot full? doesnt sound like something that would be shared.

So, you have also got 2 methods, Start and Update, these are special methods unity calls when it needs, now, its important to be aware of all the methods that unity can use, as if you try using one for a name, unity may call it when you arent expecting…

So, now you were advised to add OnDisable to your gunscript? Method order doesnt matter. So the short answer for “where”, you were told which class, you now have a good idea how your code has been structured. You should see that there are 8 places you could put that code. All work out the same.

“You also have a static variable, this means the many copies you might have would share the variable, so if one instance says your slotFull, ALL of them would… it is not a per instance variable, like the rest. This is also generally not normal, there are valid reasons, but slot full? doesnt sound like something that would be shared.”
This part makes sense why whenever I pick up another gun it will shoot its bullets too. Ill just download someone else project.

ty for taking your time to explain all of that