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; ?