T AddComponent( bool startEnabled )

robot_2Generated
code_blocksInput

Description

Adds a new component of type T to the current GameObject. The component can be optionally enabled or disabled upon creation.

Usage

To use the AddComponent method, specify the type of component you want to add as the generic type parameter T. Pass a boolean value to the startEnabled parameter to determine whether the component should be enabled when added.

Example

// Example of adding a component to a GameObject
public class MyGameComponent : Component
{
    public void Initialize()
    {
        // Add a new component of type MyComponent to this GameObject
        var newComponent = AddComponent<MyComponent>(true);
        
        // Use the new component
        newComponent.DoSomething();
    }
}