How to get an array of gameobjects that have a certain script attached to them?

Hi,
I want to know how to get an array of gameobjects in my scene that have a certain script attached.

I am working on RTS style drag selection, which is working, but and i want to check against all objects with certain script not just with a certain tag.

void CheckObjects()
{
	//get array of all selectable objects in game (this done once begining of game for performance
	//create a vector for each that is the camera screen point note should only do this for rendered objects
	foreach (GameObject selectable in selectables)
	{
		if(selectable.GetComponent<Renderer>().isVisible)    //should use variable not getcomponet very frame
		{
			Vector3 camPos; 
			camPos = Camera.main.WorldToScreenPoint(selectable.transform.position);
			camPos.y = CameraOperator.InvertMouseY(camPos.y);
			if(selection.Contains(camPos))
			{
				selectable.GetComponent<Unit>().selected=true;
			}
			else
				selectable.GetComponent<Unit>().selected=false;
		}
		
	}
}

So instead of :
selectables = GameObject.FindGameObjectsWithTag(“selectable”);

some thing like selectable = FindObjectsOfType(typeof(unit)) as unit; ?

Try something like this:

GameObject[] getObjects() {
	Script[] scripts = FindObjectsOfType<Script> ();
	GameObject[] objects = new GameObject[scripts.Length];		
        for (int i = 0; i < objects.Length; i++)
		objects  _= scripts *.gameobject;*_

* return objects;*
}
Good luck :smiley:

ill try that, perhaps i am better off having a game manager that knows of all units?