T GetComponent( bool includeDisabled )

book_4_sparkGenerated
code_blocksInput

Description

The GetComponent<T> method retrieves a component of type T from the current GameObject to which this Component is attached. This method allows you to specify whether to include components that are currently disabled.

Usage

To use the GetComponent<T> method, call it on an instance of a Component or a derived class. Specify the type of component you want to retrieve as the generic type parameter T. Pass a boolean value to the includeDisabled parameter to indicate whether disabled components should be considered in the search.

Example

// Example of using GetComponent<T> to retrieve a component
public class ExampleComponent : Component
{
    public void RetrieveComponent()
    {
        // Attempt to get a component of type MyComponent
        MyComponent myComponent = GetComponent<MyComponent>(includeDisabled: false);
        
        if (myComponent != null)
        {
            // Component found, perform operations on it
            myComponent.DoSomething();
        }
        else
        {
            // Component not found
            Log.Warning("MyComponent not found on this GameObject.");
        }
    }
}