Check which script you have a handle to.

        MonoBehaviour[] scripts = gameObject.GetComponents<MonoBehaviour>();
        foreach (MonoBehaviour script in scripts)
        {
            if (script != this  || script ==  ???)
            {
                script.enabled = false;
            }
        }

Bad brain day today…what needs filled in for ??? to NOT turn off a specific script, known by name?

Nevermind…

script.name == “name of script”

I need sleep.

Subtle detail: keep in mind that the name of a Monobehavior instance is actually the shared name of the GameObject it is on. Docs:

https://docs.unity3d.com/ScriptReference/Object-name.html

ie., if you name one Monobehavior Alice and the other Bob, and they are on the same GameObject, you will end up with two Monobehaviors with the name Bob.

Oh that is unfortunate.

So, back to the original question then. How would you identify a particular script from an array of Monobehaviours? Some TypeOf argument?

I’m confused by your question. If (script != this) then script isn’t this monobehaviour instance. If (script == this) then script is this monobehaviour instance.

        MonoBehaviour[] scripts = gameObject.GetComponents<MonoBehaviour>();
        foreach (MonoBehaviour script in scripts)
        {
            if (script == this)
            {
                //I find myself in the collection
            }
            else
            {
               //Some other script
            }
        }

If you have a specific class which inherits from MonoBehaviour and you want to turn off every script except itself and this other class, then I might do something like this.

MonoBehaviour[] mbScripts = gameObject.GetComponents<MonoBehaviour>();
List<SuperCoolBehaviour> scbScriptsList = gameObject.GetComponents<SuperCoolBehaviour>().ToList();

foreach (MonoBehaviour script in mbScripts)
{
    if ((script != this)  && (scbScriptsList.Contains(script) == false))
    {
        script.enabled = false;
    }
}

This is correct. I would like to disable every script but this one and one other.

I was hoping to avoid two GetComponent calls, but this will work.
I’ll try something similar. Thanks.

I think you are looking for the “is” keyword.

e.g.

void MyFunc(Component c)
{
    if (c is Button) do something;
    else if (c is Image) do something;
}

Note that checking whether a variable is equal to “this” is different–that’s performing a comparison on the instance, not the type. If you had 2 copies of the same script attached to an object, only one of them would compare as equal to “this”.