Inspector variable MonoScript, using for creating a List?

I have a script that checks through all gameObjects and adds the ones with a particular script to a list. It works if I use a script that is strictly defined in the Assets, but if I use the variable called script, declared as var script : MonoScript;, it throws the error “The name ‘script’ does not denote a valid type (‘not found’). Did you mean ‘UnityEditor.ScriptCallOptimizationLevel’?”.
Code:

import System.Collections.Generic;

function Start () {

}

var obj : GameObject;
var script : MonoScript;
var arrayIndex = 0;
var test : List.<script> = new List.<script>();

function Awake() {
	script = script;
	for(var t : script in GameObject.FindObjectsOfType(script))
    {
       test.Add(t);
    }
}

function Update () {
    var lineRenderer : LineRenderer = GetComponent(LineRenderer);
    
    lineRenderer.useWorldSpace = false;
    lineRenderer.SetVertexCount(2);
    
    var hit : RaycastHit;
    
    Physics.Raycast(transform.position, transform.forward, hit);
    
    if(hit.collider){
    	lineRenderer.SetPosition(1,Vector3(0, 0, hit.distance));
    	if(hit.collider.tag == "Player") {
    		test[arrayIndex].condition = true;
    		Debug.Log("Hit laser. ");
    	}
    }
    else{
        lineRenderer.SetPosition(1,Vector3(0, 0, 5000));
    }
    
}
 
@script RequireComponent(LineRenderer)

Since any script you attach to your gameObjects inherits from MonoBehaviour, you have to use a public variable of type MonoBehaviour and then you could call GetType() to get the explicit type.

var script : MonoBehaviour;
private var arr    : MonoBehaviour[]; // no need for the `test` list

function Awake()
{
   arr = GameObject.FindObjectsOfType(script.GetType()) as MonoBehaviour[];
   if (arr == null || arr.Length == 0)
      print ("No objects of type " + script.GetType() + " was found");
}

Now, let’s say you have a OtherScript script, you could assign it to the script variable via the inspector, and arr will try to find and store all OtherScripts in your scene.

But notice, since you’re doing it this way, this requires that you cast it down to something very specific since you’re now storing MonoBehaviours - In other words, if you assigned OtherScript to script and now arr has reference to all OtherScripts, in order to, for example access something from your OtherScript (like in your case the condition variable) - you’d have to do (arr[INDEX] as OtherScript).condition

But be careful, cause that cast might fail (return null), so you have to make sure it’s not null first:

var myOther = arr[INDEX] as OtherScript;
if (myOther != null) // only then
   myOther.condition = true;

If you want to get all the scripts of a specific type, you have to pass the type of the script to GameObject.FindObjectsOfType().

So if script variable is an instance of such script, and you want to get all other instances of that script type, you’d need to pass the type of the script variable to the method.

You can do that with the following code:

var allScripts = GameObject.FindObjectsOfType(typeof script);