Disable Components in array

i have made a script to pick up things. when picking something up, the script needs to disable certain components of the object you are picking up like rigidbodys and colliders. i currently have made it work like this:

if (objectHeld.GetComponent<GunSystem>())
{
    UnityEngine.Debug.Log("The gun is a raycast-based gun");
        objectHeld.GetComponent<GunSystem>().enabled = true;
}

if (objectHeld.GetComponent<ProjectileGun>())
{
    UnityEngine.Debug.Log("The gun is a projectile-Based gun");
    objectHeld.GetComponent<ProjectileGun>().enabled = true;
}

if (objectHeld.GetComponent<Rigidbody>())
{
    UnityEngine.Debug.Log("The gun has a rigidbody");
    objectHeld.GetComponent<Rigidbody>().drag = 0;
    objectHeld.GetComponent<Rigidbody>().angularDrag = 0;
    objectHeld.GetComponent<Rigidbody>().useGravity = false;
}

if ((objectHeld.GetComponent<BoxCollider>()))
{
    objectHeld.GetComponent<BoxCollider>().enabled = false;
}

if (objectHeld.GetComponent<GrappleGun>())
{
    objectHeld.GetComponent <GrappleGun>().enabled = true;
}

so this works, but it’s long and looks ugly. and if i have a new component that needs to be disabled, i need to make this mess longer. i’d rather be able to have a list or an array of objects that will be disabled, like:

foreach(Component com in componentsToBeDisabled)
{
    if(objectToBePickedUp.GetComponent(com))
    {
        objectToBePickedUp.GetComponent(com).enabled = false;
    }
}

but i cant disable components and if i try it with types, storing types in a variable doesn’t really work.

Can anyone help me?

Note also that GetComponent() is a relatively expensive operation. You should re-use the return value of GetComponent instead of calling it multiple times.

Also, know that getting a component isn’t free! You absolutely should not be repeating calls to it like this. Grab the component once then use that reference to it.

Also, you can use TryGetComponent to check if it’s there and return it if it is like so:

if (objectHeld.TryGetComponent(out Rigidbody rb))
{
    UnityEngine.Debug.Log("The gun has a rigidbody");

    rb.drag = 0;
    rb.angularDrag = 0;
    rb.useGravity = false;
}

Deactivating a GameObject will automatically disable all components on that GameObject. Maybe you could set up your GameObject hierarchy so that everything you want to disable goes on one GameObject, and then you just call SetActive(false) on that GameObject?

Hope this helps also add other components you want disabled aswell

Use this on the item

:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item : MonoBehaviour
{
public void OnPickup()
{
//Disable collider
GetComponent().enabled = false;
}
}


//And this on the player
:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerPickup : MonoBehaviour
{
[SerializeField] private float range = 3f;
[SerializeField] private KeyCode interactkey = KeyCode.E;

private void Update()
{
    if(Input.GetKeyDown(interactkey))
    {
        Pickup();
    }
}

private void Pickup()
{
    RaycastHit hitInfo;
    if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hitInfo, range))
    {
        Item item = hitInfo.collider.GetComponent<Item>();
        if(item != null)
        {
            item.OnPickup();
        }
    }
}

}