Address
304 North Cardinal St.
Dorchester Center, MA 02124

Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM

Unity AddComponent Presentation

Unity AddComponent: Enhance GameObjects

In Unity, the “AddComponent” feature simplifies the process of enhancing GameObject functionality. With a simple click in the editor’s inspector, users can effortlessly append diverse components to their Unity objects.

Table of contents

AddComponent dynamically

The `AddComponent` function allows you to add a component to a GameObject dynamically.

private void Add()
{
  var rigidbody2d = gameObject.AddComponent<Rigidbody2D>();

  // return Component, you can cast it
  var rigidbody = gameObject.AddComponent(typeof(Rigidbody2D));
}

AddComponent in Editor

You can directly add a component to your GameObject in the Unity editor by following these steps:

  1. Select your gameObject in the Scene.
  2. Click on the “Add Component” button in the inspector of your GameObject.
  3. Choose the component you want to add.

Unity AddComponent Editor

AddComponent custom script

Additionally, you can add your own script as a component. To do this, your script must inherit from the UnityEngine.Component class. In most cases, your scripts will inherit from the MonoBehaviour class, which itself inherits from Component:


using UnityEngine;

public class MyMonoBehaviour : MonoBehaviour
{

}

public class OtherMonoBehaviour : MonoBehaviour
{
    private void Awake()
    {
        gameObject.AddComponent<MyMonoBehaviour>();
    }
}

In an editor context, it may be useful to use Undo.AddComponent to implement an Undo action.

AddComponent with parameters

`AddComponent` does not natively allow passing parameters. It is preferable to create a separate function, e.g., “SetParameters,” to be called after creating the component.

using UnityEngine;

public class TorsoEquip : MonoBehaviour
{
    public int Defense { get; private set; }
    public int Resiliance { get; private set; }

    public void SetParameters(int defense, int resiliance)
    {
        Defense = defense;
        Resiliance = resiliance;
    }
}
private void AddComponentWithParamters()
{
	var torsoEqui = gameObject.AddComponent<TorsoEquip>();
	torsoEqui.SetParameters(20, 30);
}

Unity stonks

To make code more reusable, it may be wiser to pass a configuration object instead of passing parameters one by one.
This configuration object can be a class, a struct or even a ScriptableObject, depending on your needs:

using UnityEngine;

public class TorsoEquip : MonoBehaviour
{
    public class EquipementStat
    {
        public int Defense { get; set; }
        public int Resiliance { get; set; }
    }

    public EquipementStat EquipementConfiguration { get; private set; }

    public void AddConfiguration(EquipementStat equipementConfiguration)
    {
        EquipementConfiguration = equipementConfiguration;

        // now you can call 
        // EquipementConfiguration.Defense
        // Or EquipementConfiguration.Resiliance
    }
}

RequireComponent attribute

The RequireComponent attribute allows you to avoid setup errors when adding a script with this attribute. The required component will be added simultaneously, but note that this only works in the editor.
This attribute is used in “TextMeshProUGUI”, to add a RectTransform and CanvasRenderer at the same time.

textmeshpro requireComponentSource code TextMeshProUGUI

Let’s take an example: Suppose we always want our “Ball” class to have a Rigidbody2D:

using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class Ball : MonoBehaviour
{
	private Rigidbody2D _rigidbody2D;

    private void Awake()
    {
        _rigidbody2D = GetComponent<Rigidbody2D>();
    }
}

To conclude embracing Unity’s “AddComponent” feature opens a gateway to seamless customization and dynamic GameObject enhancements.
Thank you for exploring this topic with me – happy coding!

Useful resources:

Check my other articles about Unity:

Leave a Reply

Your email address will not be published. Required fields are marked *