Problem with WorldPointToScreen

I’m current working on a 3D space flight sim. I’m trying to have an objective system where you can set a waypoint with a Vector3 and it’ll display a texture when you look at that position. So right now it displays at the position I input but if you turn the camera 180 degrees the texture will show up at the same point on the screen even though the position I passed in is now behind the camera. Any advice is welcome.

here is my function:

void SetWayPoint(Vector3 target)

{     
  Vector2 position = Camera.main.WorldToScreenPoint(target);

  Vector2 screenPos = GUIUtility.ScreenToGUIPoint(new Vector2(position.x, position.y));

  GUI.DrawTexture(new Rect(screenPos.x - ObjectiveLocator.width/2, Screen.height - screenPos.y - ObjectiveLocator.height/ 2, ObjectiveLocator.width, ObjectiveLocator.height), ObjectiveLocator);

}

In the Vector3 returned by WorldToScreenPoint(), the ‘z’ value will be the world distance from the camera. It is a signed values, so negative values will be behind the camera. Untested, but I believe this will work:

void SetWayPoint(Vector3 target)
{     
  Vector3 position = Camera.main.WorldToScreenPoint(target);

  if (position.z > 0.0f)  
     {
      Vector2 screenPos = GUIUtility.ScreenToGUIPoint(new Vector2(position.x, position.y));
      GUI.DrawTexture(new Rect(screenPos.x - ObjectiveLocator.width/2, Screen.height - screenPos.y - ObjectiveLocator.height/ 2, ObjectiveLocator.width, ObjectiveLocator.height), ObjectiveLocator);
     }
}