Attach Component from a list to a GameObject

I have a list of components and I want to give each of my children one of the components in the list with it’s specific values.

public List<ItemData> lootableItems = new List<ItemData>();

I want to assign that ItemData component to an Item Prefab which already has the ItemData Component in it. So I need to swap the components in some way, so it has the updated values from the list and not the default values.

Is there a simple way to do that change?
I was thinking of something like:

item.gameObject.AddComponent(lootableItems[i]);

Thanks for the anwers!

I was thinking there might be a way to serialize the desired component and deserialize it in place on the target game object, but I can’t find anything.

Without any direct support from the Unity engine itself, my recommendation hence becomes to make something like an ICopyable interface and implement it on any component that you’d want to copy in this fashion.

// In ICopyable.cs
interface ICopyable
{
    void CopyTo(ICopyable target);
    void CopyTo(GameObject targetObject);
}

interface ICopyable<TComponent> : ICopyable where TComponent : MonoBehaviour
{
    void CopyTo(TComponent target);
}

// In SomeScript.cs
class SomeScript : MonoBehaviour, ICopyable<SomeScript>
{
    public int somePublicField;
    public int _somePrivateField;

    public void CopyTo(SomeScript target)
    {
        target.somePublicField = somePublicField;
        target._somePrivateField = _somePrivateField;
    }

    public void CopyTo(ICopyable target)
    {
        var castTarget = target as SomeScript;
        if (castTarget == null) throw new ArgumentException("The target object to copy to must be the same type as the source object.", "target");
        CopyTo(castTarget);
    }

    public void CopyTo(GameObject targetObject)
    {
        var target = targetObject.GetComponent<SomeScript>();
        if (target == null) throw new ArgumentException("The target game object must already have an instance of the source component attached to it.", "targetObject");
        CopyTo(target);
    }
}

// Elsewhere
public List<ICopyable> lootableItems = new List<ICopyable>();

// Further elsewhere
lootableItems[i].CopyTo(item.gameObject);

If that works but you’re getting sick of writing the CopyTo() functions everywhere, two of them can be extracted into a base class:

// In CopyableBase.cs
class CopyableBase<TDerived> : MonoBehaviour, ICopyable<TDerived> where TDerived : MonoBehaviour
{
    public abstract void CopyTo(TDerived target);

    public void CopyTo(ICopyable target)
    {
        var castTarget = target as TDerived;
        if (castTarget == null) throw new ArgumentException("The target object to copy to must be the same type as the source object.", "target");
        CopyTo(castTarget);
    }

    public void CopyTo(GameObject targetObject)
    {
        var target = targetObject.GetComponent<TDerived>();
        if (target == null) throw new ArgumentException("The target game object must already have an instance of the source component attached to it.", "targetObject");
        CopyTo(target);
    }
}

// In SomeScript.cs
class SomeScript : CopyableBase<SomeScript>
{
    public int somePublicField;
    public int _somePrivateField;

    public override void CopyTo(SomeScript target)
    {
        target.somePublicField = somePublicField;
        target._somePrivateField = _somePrivateField;
    }
}