Its probably very simple but i have been staring at it for some time so figured I’d ask for some help. Basically I am trying to see if a gameobject is close to the player. I do that using an array thats filled with gameobjects with a certain and im looping through them. The problem, however, is when i try to test it it picks only one gameobject instead of all of them which makes sense but i cant think of a way to fix it.
Code:
void CheckIfItemIsPickable(){
for (int x = 0; x < pickableItems.Length; x++) {
Vector3 screenPoint = cam.WorldToViewportPoint (pickableItems [x].gameObject.transform.position);
bool onScreen = screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;
float distBetweenPlayerAndItem = Vector3.Distance (transform.position, pickableItems[x].gameObject.transform.position);
if (distBetweenPlayerAndItem < minDist && onScreen) {
pickUpText.SetActive (true);
} else {
pickUpText.SetActive (false);
}
}
}