---
tags:
    - "Get"
    - "Add"
    - "Ensure"
---

# Require Component

[Game Object](../index.md) / [Component](index.md) / **Require Component**

## Description

Checks if a Game Object has a specific component and adds it otherwise

## Parameters

| Name | Description |
| :--- | :---------- |
| Game Object | The game object reference |
| Component | The type of component to make sure the game object has |

## Code

```cs
using System;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Title("Require Component")]
    [Image(typeof(IconCubeOutline), ColorTheme.Type.TextLight, typeof(BadgeDot))]
    
    [Description("Checks if a Game Object has a specific component and adds it otherwise")]
    [Category("Game Object/Component/Require Component")]
    
    [Parameter("Game Object", "The game object reference")]
    [Parameter("Component", "The type of component to make sure the game object has")]
    
    [Keywords("Get", "Add", "Ensure")]
    
    [Serializable]
    public class InstructionCoreGameObjectRequireComponent : Instruction
    {
        [SerializeField] private GetGameObject m_GameObject = new GetGameObject();

        [SerializeField] private TypeReferenceComponent m_Component = new TypeReferenceComponent();

        protected override string GetTitle(GameObject source)
        {
            string component = TextUtils.ThemeAsConstant(this.m_Component.ToString());
            return $"Require {component} in {this.m_GameObject}";
        }

        protected override Awaitable Run(Args args)
        {
            GameObject gameObject = this.m_GameObject.Get(args);
            if (gameObject != null)
            {
                gameObject.Require(this.m_Component.Type);    
            }
            
            return Done;
        }
    }
}
```

