how to read in game objects in a list from different script

hey, i’m following a new tut for a RTS game. But there’s only explained how to select each single object. So i fighted me through several tuts, and found a script attached to a unit prefab which detects if it’s selected by drawing a rectangle for multi selection.
It basically works but i haven’t found a way to read in those units in a List in a script which is attached to the maincam with witch the rectangle is drawn and there i declared the List.

This script is used with the main cam :

public Texture aTexture;
public static Rect selection = new Rect(0,0,0,0);
private Vector2  _box_start_pos = Vector2.zero;
private Vector2 _box_end_pos = Vector2.zero;
public string Pl_tag = "Player"; 
public List<GameObject> selectedObj = new List<GameObject>();

void Update ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit; 
        Debug.DrawRay(ray.origin, ray.direction * 100.0f, Color.yellow);
// Called while the user is holding the mouse down.
        if(Input.GetKey(KeyCode.Mouse0))
        {
            if (Physics.Raycast(ray, out hit ))
            {
                //Debug.Log(hit.collider.tag  );
                if(hit.collider.tag == Pl_tag)
                {
                    var obj = hit.collider.gameObject;

                    if(selectedObj.Contains(obj) == false)
                    {
                        selectedObj.Add(obj);
                       
                    }
                    Debug.Log(Pl_tag +" hit");
                }
            }
 else
            {
                selectedObj.Clear();
                Debug.Log("");
            }
// Called on the first update where the user has pressed the mouse button.
            if (Input.GetKeyDown(KeyCode.Mouse0))
                _box_start_pos = Input.mousePosition;
            else  // Else we must be in "drag" mode.
                _box_end_pos = Input.mousePosition;    
        }
        else
        {
            // Handle the case where the player had been drawing a box but has now released.
            if(_box_end_pos != Vector2.zero && _box_start_pos != Vector2.zero)
                //HandleUnitSelection();

            // Reset box positions.
            _box_end_pos = _box_start_pos = Vector2.zero;
        }
/// <summary>
    /// Draws the selection rectangle if the user is holding the mouse down.
    /// </summary>
    void OnGUI()
    {
        // If we are in the middle of a selection draw the texture.
        if(_box_start_pos != Vector2.zero && _box_end_pos != Vector2.zero)
        {
            // Create a rectangle object out of the start and end position while transforming it
            // to the screen's cordinates.
            selection = new Rect(_box_start_pos.x, Screen.height - _box_start_pos.y,
                                  _box_end_pos.x - _box_start_pos.x,
                                   -1 * (_box_end_pos.y - _box_start_pos.y));
            // Draw the texture.
            GUI.DrawTexture(selection, aTexture);
        }
    }
 public static float InvertMouseY(float y)
        {
            return Screen.height - y;
        }  

This script is attached to the GameObject prefab called Unit:

   public GameObject  thistransform;
    public bool selected = false;
    private Renderer rend ;
   

    void Start()
    {
        Renderer rend = thistransform.GetComponent<Renderer>();
    }
	void Update () 
    {
       
        Renderer rend = thistransform.GetComponent<Renderer>();
        if(rend.isVisible && Input.GetMouseButtonUp(0))
        {
            Vector3 camPos = Camera.main.WorldToScreenPoint(transform.position);
            camPos.y = NewBehaviourScript.InvertMouseY(camPos.y);
            selected = NewBehaviourScript.selection.Contains(camPos, true);
        }

        if(selected)
        {
            rend.material.color = Color.red;  
        }
        else
            rend.material.color = Color.green;
       
	}  

So i would like to achiefe to read this game objects in the List selectedObj. If i understand it right selection.Contains(camPos, true) does return only Vector3 of each object ?
I’ve tried very much but i’m out of any idea now.
I hope that all isn’t to confusing the way i explained it here (i’m german)

so many thanks previously !!!

I kept on trying and i found a solution with the “FindGameObjectsWithTag” command. Therefore i tagged a few units with “Player”.
In the first script attached to the maincam i’added :

public string Pl_tag = "Player";   
public GameObject[] Arrayselobjects; 

In the Update() i’ve added :

   Arrayselobjects = GameObject.FindGameObjectsWithTag("Player"); // buildin array
    foreach(GameObject arrayselobj in Arrayselobjects )
    {
    bool isselected = arrayselobj.GetComponent<Unit>().selected;

        if(isselected)
        {
            if(selectedObj.Contains(arrayselobj) == false)
            {
                selectedObj.Add(arrayselobj); // the objects in the array are now read in the List
            }
        }
    }  

At the end of the Update :

Arrayselobjects = null; // this clears the array 

In the script for each Unit i changed :

  if(selected && gameObject.tag == "Player")
            {
                rend.material.color = Color.red;  
            }
            else
            {
                rend.material.color = Color.green;
            }  

So all in my test scene works well but i think its not the very best way for perfomance because using a Generic List and FindGameObjects costs a lot of perfomance !
All more advanced suggestions which aren’t that expensive for perfomance are welcome ! :slight_smile:

yours