# Why is this not working?

**URL:** <https://forums.unrealengine.com/t/why-is-this-not-working/480681>\
**Category:** C++\
**Tags:** logic, unreal-engine, CPP, question\
**Created:** [March 24, 2021, 9:41am UTC](https://forums.unrealengine.com/t/why-is-this-not-working/480681 "2021-03-24T09:41:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![User00016322](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@User00016322](https://forums.unrealengine.com/u/User00016322)\
**Post date:** [March 22, 2021, 1:33pm UTC](https://forums.unrealengine.com/t/why-is-this-not-working/480681/1 "2021-03-22T13:33:36Z")

</div>

ok, I figured it out!  
  
  
just adding a simple break statement WORKED!  
  
  
here’s my new code:

```
TArray overlaps;
	GetCapsuleComponent()->GetOverlappingActors(overlaps);
	float closestDist = INFINITY;

	for (int i = 0; i < overlaps.Num(); i++) 
	{
		AWeaponActor* castActor = Cast(overlaps[i]);
		if (castActor)
		{
			float dist = this->GetDistanceTo(castActor);
			closestDist = FMath::Min(dist, closestDist);
			if (dist == closestDist)
			{
				CurrentStoredMeshData = castActor;
				castActor->_WeaponUI->SetVisibility(ESlateVisibility::Visible);**break; } else { castActor-\>\_WeaponUI-\>SetVisibility(ESlateVisibility::Hidden); } } }**
```

---

<div class="post-metadata">

**Author:** ![User00016322](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@User00016322](https://forums.unrealengine.com/u/User00016322)\
**Post date:** [March 24, 2021, 9:41am UTC](https://forums.unrealengine.com/t/why-is-this-not-working/480681/2 "2021-03-24T09:41:07Z")

</div>

So, I am having a very strange problem,  
All I am trying to do is find the closest overlapped weapon actor to the player and display it’s UI but it’s not working  
here is the code:

```
TArray overlaps;
	GetCapsuleComponent()->GetOverlappingActors(overlaps);
	float closestDist = INFINITY;

	for (int i = 0; i < overlaps.Num(); i++) 
	{
                 //My weapon class
		AWeaponActor* castActor = Cast(overlaps[i]);
		if (castActor)
		{
			float dist = this->GetDistanceTo(castActor);
			closestDist = FMath::Min(dist, closestDist);
			if (dist == closestDist)
			{
                                //storing the weapon actor
				CurrentStoredMeshData = castActor;
				castActor->_WeaponUI->SetVisibility(ESlateVisibility::Visible);
			}
			else 
			{
				castActor->_WeaponUI->SetVisibility(ESlateVisibility::Hidden);
			}
                       /* I also tried this but it does not work:
			if (dist < closestDist)
			{
                                closestDist = dist;
                                //storing the weapon actor
				CurrentStoredMeshData = castActor;
				castActor->_WeaponUI->SetVisibility(ESlateVisibility::Visible);
			}
			else 
			{
				castActor->_WeaponUI->SetVisibility(ESlateVisibility::Hidden);
			}

                       */
		}
	}
```

Here is what happens when I try it out in the editor PIE:  
(both the UI widgets should not be visible, only the closest one should be visible)

![333098-prob.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/1/4/914030049302cafa94280ef3680df4521ca1e9be.png)

  
Did I do this properly? Is there any other or better way I should do it?
