zaro

How to Disable a Component of a GameObject in Unity

Published in Unity Component Control 3 mins read

To disable a component of a GameObject in Unity, you typically set its enabled property to false. This can be done through a script or directly within the Unity Editor's Inspector window.

Disabling Components via Script

Disabling components dynamically from a script is a common practice for controlling game logic, performance, and behavior based on specific events or conditions. As demonstrated in resources like the video "C# Enabling and Disabling Components in Unity! - YouTube", you can manipulate the enabled state of components directly in your C# code.

Accessing the Component

First, you need a reference to the component you want to disable. You can get this reference using the GetComponent method on the GameObject the component is attached to.

For example, to disable a Collider component:

Collider myCollider = GetComponent<Collider>();
if (myCollider != null)
{
    myCollider.enabled = false; // Disable the collider
}

Or to disable a custom script component named MyCustomScript:

MyCustomScript scriptToDisable = GetComponent<MyCustomScript>();
if (scriptToDisable != null)
{
    scriptToDisable.enabled = false; // Disable the script
}

Example Script Structure

Here is a simple example of a script that disables another component (e.g., a Renderer) on the same GameObject when the game starts:

using UnityEngine;

public class ComponentDisabler : MonoBehaviour
{
    public Renderer targetRenderer; // Drag the Renderer component here in the Inspector

    void Start()
    {
        // Alternative: Get the component directly if it's on the same GameObject
        // targetRenderer = GetComponent<Renderer>();

        if (targetRenderer != null)
        {
            // Disable the target Renderer component
            targetRenderer.enabled = false;
            Debug.Log("Renderer component disabled.");
        }
        else
        {
            Debug.LogError("Target Renderer not assigned or found!");
        }
    }
}

Attach this script to a GameObject that also has a Renderer component. Then, drag the Renderer from the same GameObject onto the Target Renderer field in the Component Disabler script's Inspector slot. When you run the game, the script will disable the Renderer.

Practical Considerations

  • When a component is disabled, it stops performing its function (e.g., a disabled Renderer won't draw the object, a disabled Collider won't detect collisions, a disabled MonoBehaviour script stops executing Update, FixedUpdate, etc.).
  • The component instance still exists in memory. Disabling it is different from destroying it.
  • You can re-enable a component by setting its enabled property back to true.

Disabling Components in the Unity Editor

For components that you want to be disabled by default or for testing purposes, you can disable them directly in the Unity Editor without writing any code.

Here's how:

  1. Select the GameObject in the Hierarchy window.
  2. Look at the Inspector window, which shows all components attached to the selected GameObject.
  3. For the specific component you want to disable, locate the small checkbox next to its name at the top of the component panel.
  4. Uncheck this box.

The component is now disabled and will not be active when the game runs or while in the editor (depending on the component type). You can re-enable it at any time by checking the box again.

This method is straightforward for static disabling but cannot be used for dynamic control during gameplay, which is where the scripting method is essential.


In summary, disabling a component gives you granular control over your GameObject's behavior and resource usage. Whether using the convenient Inspector checkbox for initial setup or leveraging scripts for dynamic control, the core principle is setting the component's enabled state.