unity gameobject addcomponent C# script code example
Example 1: unity add component
//Use the AddComponent<T>() Method to add a component to your gameobject
GameObject gameObject;
gameObject.AddComponent<BoxCollider>(); //Component has to be an actual Unity component
Example 2: c# addcomponent
// Consider an existing GameObject, gameObj
Rigidbody addedComp = gameObj.AddComponent<Rigidbody>() as Rigidbody;
// You can also add custom components by class name