Scripting Reference

◆ AddComponent< TComponent, TArgument >() [1/2]

static TComponent Sisus.Init.GameObjectExtensions.AddComponent< TComponent, TArgument > ( [NotNull] this GameObject  gameObject,
TArgument  argument 

Adds a component of type TComponent to the gameObject and initializes the component using the provided argument .

The argument should either be received by the added component during its initialization (such during the Awake event function or in the constructor) or if the component class implements the IInitializable<TArgument> interface the argument can be provided using the Init function immediately after initialization has finished (before the Start event function).

For classes deriving from MonoBehaviour<TArgument> the latter method will be used in cases where the gameObject is inactive, while if the the gameObject is inactive the arguments will be received during the Awake event function.

Template Parameters
TComponentType of the component to add.
TArgumentType of the argument passed to the component's Init function.
gameObjectThe GameObject to which the component is added.
argumentThe argument passed to the component's Init function.
The added component.
ArgumentNullExceptionThrown if this GameObject is null.
InitArgumentsNotReceivedExceptionThrown if TComponent class does not implement IInitializable<TArgument> and did not manually handle receiving the provided argument.
Type Constraints
TComponent :MonoBehaviour 
TComponent :IArgs<TArgument>