C# Enable component on another GameObject using name of component

I spent many hours on finding an answer but no success. Thing is simple. After timeOut, a component on another gameobject will become active. It looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TimedObjectEnableComponent : MonoBehaviour {

    public float timeOut = 1;
    public GameObject go;
    public string NameOfComp;

    void Awake()
    {
            Invoke("EnableComponent", timeOut);
    }

    void EnableComponent()
    {
        go.GetComponent<NameOfComp>().enabled = true;
    }
}

But it result with error CS0118: TimedObjectEnableComponent.NameOfComp' is a field’ but a `type’ was expected. Any help would be appreciated.

Yes. The problem is exactly what the error describes. You are passing a string where you should be passing type instead. Type is a special class whose instances can represent any object’s type and this object (the type one) is NOT the same thing as a string thus the error. The version of the GetComponent method that you are using expects a type, so what you need to do is convert your string into a valid type before passing it.

To convert your string into a type you will need to use a bit of reflection.

void EnableComponent()
{
    Type compType = Type.GetType(NameOfComp);
    go.GetComponent(compType).enabled = true;
}

Keep in mind that GetType only works for classes in the same assembly. If your class is in another assembly you will also need to specify the name of that assembly. If your class is inside a namespace you will need to specify the namespace as well.