casting unknown type in c#

How do you do, in c#
var script = gameObject.GetComponent (scriptName)
where ScriptName is a public string the user fills in

Later on we want to do script.enabled = true; and an type object doesn’t have this variable…

MonoBehaviour is the base class of all scripts, and it has an .enabled member, so you should be able to cast to that to get the effect you want:

MonoBehaviour script = something.GetComponent("ScriptName") as MonoBehaviour;
script.enabled = false;

I see that you cannot drop a script from the asset panel to the inspector monobehaviour variable…
The script needs to be instance aparently
what i want to do is compare a mono variable to an array of mono types, do i need to resort to string matching? if so mono.name returns the name of the GO the mono is attached to, not the name of the mono itself. is there a way to get this name?

public MonoBehaviour script;
public MonoBehaviour[] acceptedScripts;

void Start ()
{
    foreach (MonoBehaviour s in acceptedScripts)
         if (script.name == s.name) {}    
}

Just for the record, as I faced the same problem: script.ToString() returns the name of the GO and the name of the script in parenthesis.

It’s a different syntax but it’s quite accurate and extensible to use Type objects

public MonoBehaviour script;
public MonoBehaviour[] acceptedScripts;

void Start ()
{
    for(int i = 0; i < acceptedScripts.Length; i++)
         if (acceptedScripts[i].GetType() == s.GetType()) {}    
}

Type objects are available for all objects, and will define what type it is, as well as tons of reflection information for fields and methods.

You can also use the is/as keyword

if(script is MonoBehaviour){//Do Something}

You could even reduce the amount of code and do something like this:

something.GetComponent("ScriptName").enabled = false;

As a nitpicky sorta thing it’s also faster and requires less casting if you use Generics

gameObject.GetComponent<ScriptName>().enabled;

Using the <> allows you to get the component already as the component type, rather than as a MonoBehaviour.

The good purposes for the string variation are for creating components you don’t know the type at compile time:

string scriptName =  myScript.GetType().FullName;
gameObject.GetComponent(scriptName);

Allows you to make a few more dynamic options for getting/adding components.

Good point. I forgot that the GetComponent() method was templatized.

Actually, it’s

string scriptName =  myScript.GetType().Name;
gameObject.GetComponent(scriptName);

instead of what you wrote, “Name” instead of “FullName” being the important part here. And here’s why: .FullName returns the component type’s name INCLUDING its namespace. In case of a Rigidbody component the returned string would be “UnityEngine.Rigidbody”. But for some reason, GetComponent doesn’t accept the string if it includes the namespace and returns null instead of the expected Rigidbody component. Leaving the namespace out however returns the expected result. That’s why you should use .Name instead of .FullName.