Question by
zaczacattack · Mar 06, 2016 at 10:40 AM ·
arrayarray of gameobjectsfindgameobjectswithtag
Check if transform.position matches position of any object in a given array
I have this:
private GameObject handL;
private GameObject[] handLtargets;
void Start()
{
handL = GameObject.Find("hand_L");
handLtargets = GameObject.FindGameObjectsWithTag("hand_L_target")
}
Now I want an if statement that becomes true when handL.transform.position is the same as any of the positions of the objects in the handLtargets array. Not sure how to go about this.
Comment
Best Answer
Answer by MichalOp · Mar 06, 2016 at 01:11 PM
You can use this function, it returns true when any of the objects is in the same position as hand and false in other case:
void Start()
{
handL = GameObject.Find("hand_L");
handLtargets = GameObject.FindGameObjectsWithTag("hand_L_target")
if(IsPositionMatching()){
print("hand position is the same as position of something in the list");
}
}
bool IsPositionMatching(){
foreach(GameObject handTarget in handLtargets ){
if(handtarget.transform.position == handL.transform.position)
return true;
}
return false;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612070804im_/https://answers.unity.com/themes/thub/images/avi.jpg)