Array foreach problem

if (target.Length > 0)
{
foreach (var item in target)
{

                wantedPos = Camera.main.WorldToScreenPoint(item.position);

                if (wantedPos.z > 0 && wantedPos.x < Screen.width / 2 + circleRect.rect.width / 2 && wantedPos.x > Screen.width / 2 - circleRect.rect.width / 2
              && wantedPos.y < Screen.height / 2 + circleRect.rect.height / 2 && wantedPos.y > Screen.height / 2 - circleRect.rect.height / 2)
                {
                    wantedPos.z = 0;
                    wantedPos.y = Mathf.Clamp(wantedPos.y, Screen.height / 2 - circleRect.rect.height / 2, Screen.height / 2 + circleRect.rect.height / 2);
                    wantedPos.x = Mathf.Clamp(wantedPos.x, Screen.width / 2 - circleRect.rect.width / 2, Screen.width / 2 + circleRect.rect.width / 2);
                    transform.position = wantedPos;
                }
                else
                {
                    transform.position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
                }
            }
        }

when there are several targets this code works for only one of them. what is the point that I’m missing here?

Well you have a logic error in your code. You iterate through all your targets. In each case you either set it to your calculated “wantedPos” or you set it to the center of the screen. So no matter what the result of your if statement you will change the object’s position every time. That means only the last target in the array will actually have any effect because that’s the last one that is checked.

Since we have no idea what this code is about or what it should do, we can not tell you what you should change. We just see what it currently does.