trying to create a selection list of game objects

So i’m working on a 2d real time strategy game. Currently i’m working on the selection controls, the player can select units, these units for now at east have a “selection_test” script attached, i’m trying to run the script upon selection, as well as adding it to a list (in another script) and displaying the list on the ui. I have got the ray-cast working , this lets me grab a reference to a unit, but i’m trying to turn the reference, into a game object and then I add it to the list.

see attachments for scripts
Select_mang is were the list is, it also handles the ray cast
Selection_test is the script on my unit

I get an error when I try to list off the selected units in console, and when It tries to run a function in selection_test, these errors appear when the game is ran, not in Visual studios

5296098–531687–Select_Mang.cs (1.47 KB)
5296098–531690–Selection_test.cs (410 Bytes)

It’s easier to review your code if we don’t have to download your files. Try pasting them directly into the post using code tags or the insert code button so they are embedded in your post.

What is the exact error message you’re seeing? Which file & line number does it point to?

k, the error is attached, it happens when I ray-cast to select a unit. code is also here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Select_Mang : MonoBehaviour
{
    public GameObject unit;
    private List<GameObject> Selected;


    // Start is called before the first frame update
    void Start()
    {
        Selected = new List<GameObject>();
    }
    // Update is called once per frame
    void FixedUpdate()
    {


        if (Input.GetMouseButtonDown(0))
        {
            Vector2 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D hit = Physics2D.Raycast(mousepos, Vector2.zero, 0f, 1 << 8);
            if (hit.collider != null)
            {
                unit = hit.collider.gameObject;
                Selection_test IsSelected =unit.GetComponent<Selection_test>();
             
                Debug.Log("hit detected");

                if (Input.GetKey("left ctrl"))
                {
                    Selected.Add(unit);
                   
                    IsSelected.ifSelected();


                }
                else
                {
                    Selected.Clear();
                    Selected.Add(unit);
                    IsSelected.ifSelected();
                }
            }

            foreach (var go in Selected)
            {
                Debug.Log(go.name);
            }

        }
    }
}

I don’t see the error. If you get a nullreference exception when trying to print the names, then you have a null gameobject in your list. If you’re destroying any selected unit gameobject during the game, you will have empty entries in your list.

Also try nullchecking “IsSelected” before using it, any GetComponent call can return null.