Hello!
I got a raycast, which I use to detect 2 different objects. Now I want to count, how many objects are in one raycast. That’s my script (or parts of it):
public class RaycastWin : MonoBehaviour {
int rightHitsToWin = 1;
void Update() {
var transformStone = GetComponent();
Vector2 right = transform.TransformDirection(Vector2.right) * 5;
Ray2D rightRay = new Ray2D(new Vector2(transformStone.position.x + 1, transformStone.position.y), right);
RaycastHit2D rightHits = Physics2D.RaycastAll(rightRay.origin, rightRay.direction, 2);
foreach (RaycastHit2D rightHit in rightHits) {
if (gameObject.CompareTag(“orange”)) {
if (rightHit.collider.CompareTag(“orange”)) {
Debug.Log(“HIT! OrangeRight”);
rightHitsToWin++;
}
}
if (gameObject.CompareTag("blue")) {
if (rightHit.collider.CompareTag("blue")) {
Debug.Log("HIT! BlueRight");
rightHitsToWin++;
}
}
}
}
}
So, as you see, in my script I count every frame ++ one and the same object. How can I count one object only once?
Thanks in Advance