Can you Pass a string into GetComponent?

When using get component, is it possible to pass in a string for the name of the component? Currently, I get a number of different errors like

Assets/Scripts/HitBox.cs(36,76): error CS0246: The type or namespace name `aComponent’ could not be found. Are you missing a using directive or an assembly reference?

This is what I want to do.

example:

//of course this would result in a loop, but it's the proper syntax i'm worried about

void update (){
    aFunction(theComponentIWantToAccess);
}


void aFunction(string aComponent){
     transform.root.GetComponent<aComponent>().anInt++;
}

I get the same issue when I use parentheses instead of <>

i.e.
GetComponent(aComponent)

After spending a few hours searching, I found the solution. Here is a small script that I made :slight_smile:

var MyScriptNames: String[];

    function Start(){
    
    for(var i : int = 0; i < MyScriptNames.Length; i++)
        {
     	(gameObject.GetComponent(MyScriptNames*) as MonoBehaviour).enabled = true;*
  • }*
    }

From the docs:

ScriptName other;
other = gameObject.GetComponent("ScriptName") as ScriptName;