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.
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.
Use this method when you need to access a specific component attached to the same GameObject
as the current component. You can choose to include disabled components in the search by setting the includeDisabled
parameter to true
.
// Example of using GetComponent to retrieve a component public class ExampleComponent : Component { public void ExampleMethod() { // Retrieve a component of type MyComponent MyComponent myComponent = GetComponent<MyComponent>(includeDisabled: false); if (myComponent != null) { // Use the retrieved component myComponent.DoSomething(); } } }