Need a little help with this nullrefexception.

So these pop up if something is missing a reference, but it’s sometimes difficult to see what exactly the problem is. So I’ll quickly explain my script:

Left clicking on a selectable object (predicated by the public list), will cause selected to be true and return that selectable object’s corresponding ‘target’ gameobject. I’m moving the selectable objects around using target go’s for them to follow. The right clicking will move the chosen target to the mouse cursor position.

The nullreference exception that occurs at runtime refers to this line: target.transform.position = hit.point;

public class Movement : MonoBehaviour {

    //list of the targets to move
    public List<GameObject> targets = new List<GameObject>();

    //list of the gameobjects to be selected
    public List<GameObject> units = new List<GameObject>();

    public bool selected = false;


    public Camera camera;
    private GameObject target;


    

    void Update () {

        Select();

        if (selected == true)
        {
            SetTarget();
        }
    }

    public GameObject Select()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit))
            {
                foreach (GameObject unit in units)
                {
                    if (hit.collider.gameObject == unit)
                    {
                        selected = true;
                    }
                    else { selected = false; }
                }
                foreach (GameObject targ in targets)
                {
                    if (hit.collider.gameObject.tag == targ.tag)
                    {
                        return targ;

                    }


                }



            }

        }
        return null;


    }


    public void SetTarget()
    {
        Debug.Log("selected");
        
        target = Select(); 

            if (Input.GetMouseButtonDown(1))
            {
                RaycastHit hit;
                Ray ray = camera.ScreenPointToRay(Input.mousePosition);

                //Raycast
                if (Physics.Raycast(ray, out hit))
                {
                    target.transform.position = hit.point;
                }
            }

    }
}

Is the if statement for this:

if (hit.collider.gameObject == unit)
                     {
                         selected = true;
                     }

returning true and therefore exiting? If so, how would I get around this?

You need to make sure that the target is not null before assigning the position of it to the hit point. Since Select() can return null, when it does, you will be attempting to access the transform and set the position of a null object. One way to fix this is to do a null check at this line here:

//Raycast and ensure target returned from select was not null
 if (Physics.Raycast(ray, out hit) && target != null)
 {
      target.transform.position = hit.point;
 }