> For the complete documentation index, see [llms.txt](https://infinitypbr.gitbook.io/magic-pig-games/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://infinitypbr.gitbook.io/magic-pig-games/northstar-nav-and-tracking/overview-and-quickstart/tips-and-code-examples.md).

# Tips & Code Examples

## Getting an `OverlayIcon` at Runtime

If you're customizing things, such as adding a UI Health Bar to your icons, you may need to access the `OverlayIcon` component at runtime associated with your object, but it won't be created until the game starts.

We can use a coroutine to grab this just a couple frames after `Start()`. You can also do additional logic and plumbing at that point as well.

<pre class="language-csharp"><code class="lang-csharp">// In this example, we are grabbing the OverlayIcon associated with the
// Screen &#x26; Edge Overlay. You can also grab the NavigationBarIcon from
// the TrackedTargetOverlay if you wish.

public class MyScript : MonoBehaviour
{
    private TrackedTargetOverlay _trackedTargetOverlay; 
    <a data-footnote-ref href="#user-content-fn-1">private</a> OverlayIcon _overlayIcon;
    
    private void Awake()
    {
        _trackedTargetOverlay = GetComponent&#x3C;TrackedTargetOverlay>();
        StartCoroutine(RegisterOverlayIcon());
    }
    
    private IEnumerator RegisterOverlayIcon()
    {
        while (_overlayIcon == null)
        {
            _overlayIcon = _trackedTargetOverlay.ScreenIcon;
            if (_overlayIcon != null)
            {
                // Do additional actions here
                yield break;
            }
            yield return null;
        }
        yield break;
    }
}
</code></pre>

## Get Distance to the Target

Assuming you have access to the `OverlayIcon` as shown above, you can get the distance to the target. Additional members can be found on the [**Overlay Icon**](broken://pages/KJxASr9Wkp8vRDut3uxR) page.

```csharp
var distanceToTarget = _overlayIcon.DistanceToTarget;
```

## Check if Icon is on Screen, on Edge, or in Transition

The `OverlayIcon` handles both screen and edge overlay, and there's a period between the two where it will transition.

```csharp
// 0 = Fully on screen
// 1 = Fully on edge
// Otherwise, it's in transition
var PercentToEdge => _overlayIcon.PercentToEdge;
var OnScreen => PercentToEdge == 0;
var OnEdge => PercentToEdge == 1;
var InTransition => PercentToEdge != 0 && PercentToEdge != 1;
```

[^1]:
