How to add components using script in Unity

Unity works based on a component system. Every Gameobject’s characteristics are defined by the components added to it. For example, the position is defined by the transform component and the collision is defined by the collider component. This component-based system requires a constant addition or removal of components to alter the behavior of the Gameobjects. In this tutorial, we will see how to add a new component to a Gameobject and also how to access and modify the existing components.

Adding component using script

Adding component to a Gameobject during gameplay is not the right way to manage your code, adding a new component takes up a lot of memory and if you frequently add or remove a component from a Gameobject during gameplay, there will be performance issues. We will discuss on how to to efficiently work with components in the later part of this tutorial.

Adding component to the same Gameobject where the script is attached.

using UnityEngine;
using System.Collections;

public class AddCompExample : MonoBehaviour
{
    void Update()
    {
      if(condition)
      {  
       this.gameObject.AddComponent<Rigidbody>(); 
      }
    }
}

Adding component to a different Gameobject

using UnityEngine;
using System.Collections;

public class AddCompExample : MonoBehaviour
{
public Gameobject ball;
    void Update()
    {
       if(condition)
       { 
         ball.AddComponent<Rigidbody>(); 
       }
    }
}

Remove a component using script in Unity

Unlike addcomponent, Unity does not have a remove component function. You can use the destroy function for this purpose. Be careful or you might end up destroying the whole Gameobject or the script.

using UnityEngine;
using System.Collections;

public class removeCompExample : MonoBehaviour
{
    Rigidbody rig;
    
    void Start()
    {
     rig=this.gameObject.GetComponent<Rigidbody>();

    void Update()
    {
       if(condition)
       { 
         Destroy(rig); 
       }
       
    }
}

How to efficiently add or remove component

As we discussed above, adding or removing component during run-time will affect the performance of your game. So, how to go about it? This is very similar to instantiating an Gameobject. The best way is to add the component in the editor and disable it. You can enable the component when required and disable when not required.

Adding a component using Unity editor

Select the Gameobject and click Add component in the inspector window.

Unity Addcomponent window

Enable and disable component script

Get the attached component and then enable it when required. One thing to note here is, the GetComponent function works even with disabled components.

using UnityEngine;
using System.Collections;

public class enableCompExample : MonoBehaviour
{
    Rigidbody rig;
    
    void Start()
    {
     rig=this.gameObject.GetComponent<Rigidbody>();

    void Update()
    {
       if(component_required)
       { 
         rig.enabled=true; 
       }
       else if(component_not_required)
       {
         rig.enabled=false; 
       }
       
    }
}

Hope this tutorial answered all your questions regarding adding and removing a component from your Unity Gameobject. If you have any more questions, feel free to leave a comment below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.