Closest Target Vector Check

So, in my design, my character drops up to 3 bombs at a time. These bombs are able to be picked up if the player comes close enough. Overall I have this working, but not entirely. The script constantly checks for any new bombs added to the scene and places them into an array and it then continues to check to see if the bomb is in range of the player to be grabbed.

If I drop the first bomb, it works with no problems. But, if I drop a second or third bomb, the code converts the closest object to one of those new bombs that may be closest, which is fine. But, if I run away from the most recently checked close bomb and go to one of the previous layed ones, it doesnt recheck that those are again the closest. Sorry if this doesnt make sense, but heres the code to help clear it up.

As you will see, if the bomb comes within the closestTarget range, which I have set to 1, it will assign that bomb to the variable. And if I move out of the 1 meter range, it will nullify the variable. If I go back into range, it calls it back. My issue is that if I lay another bomb and it takes the new closestTarget variable, the old one cant be reassigned.

void Update() 
	{
		bomb = GameObject.FindGameObjectsWithTag("Bombs");
		
		for(int i = 0; i < bomb.Length; i++)
		{
			if(Vector3.Distance(this.transform.position, bomb*.transform.position) <= closestDistance)*
  •  	{*
    

_ closestTarget = bomb*;_
_
bombPickUp = true;_
_
}*_

_ if(Vector3.Distance(this.transform.position, bomb*.transform.position) >= closestDistance)
{
closestTarget = null;
bombPickUp = false;
}*_

* }*

* //If true, key press accessable*
* if(bombPickUp)*
* {*
* if(Input.GetKeyDown(ā€œcā€))*
* { *
* //Pick up the bomb*
* StartCoroutine(CarryBomb());*
* }*
* }*
}

Hi!
You have some problems with your looping logic.
the loop goes thru all the bombs in the bomb array, but it will not get out of the loop until it has finished processing all your bombs.
Depending on what the distance of the last bomb is, closestTarget and bombBickup will be set.

also where is closestDistance set?

    //dont repeatedly set this, do it once!
closestDistance=1;// or whatever is an acceptable max value
              closestTarget = null;
              bombPickUp = false;
         
    for(int i = 0; i < bomb.Length; i++){
     if(Vector3.Distance(this.transform.position, bomb*.transform.position) <=closestDistance)*

{
closestTarget = bomb*;*
bombPickUp = true;
//maybe set closestdistance here to make only the closest bomb pickable ?
closestDistance=Vector3.Distance(this.transform.position, bomb*.transform.position);*
}

ā€¦