Toggle script that is on another object.

Hello, I want to write script so that when I grab a weapon (When I press 1 on keyboard or any other key)
to disable a script that is on another weapon… I already have written system of grabbing weapons…
I had a problem with dealing damage to my enemy because all damages of my weapons are added in between and thats why I wanted to make that script…

using UnityEngine;
using System.Collections;
public class Pistol : MonoBehaviour
{
    public int theDamage;
   
    void Update()
    {
        RaycastHit hit;
        EnemyHealth healthComponent;

        Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

        if (Input.GetMouseButtonDown(0))
        {
            if (Physics.Raycast(ray, out hit, 100))
            {
                // gets a reference to the component!
                healthComponent = hit.collider.GetComponent<EnemyHealth>();

               
                if (healthComponent == null)
                    return;

                healthComponent.ApplyDamage(theDamage);
                Debug.Log("Working");
            }
           
        }
    }
}
previousWeapon.GetComponent<Pistol>().enabled = false;

Unless all your weapon scripts are using Pistol, I would suggest you consider a base class like “Weapon” or an interface like “IWeapon”. Or, just make a common script name for all weapons and just change the data on them instead.

okey… but I would rather go with a toggle …How to toggle another script with the script?

Well, what do you think a toggle is? It’s usually two states. On or off. So you turn on or off the weapon script. That’s exactly what the code I showed you does. I suppose you could have a bool in Update that you change to true/false instead and if it’s false, it just returns.

Unless you have a different meaning for the word toggle, in which case I’d need more info on your design.