If statement calls itself randomly?

Hey guys! I’ve been having this bug for a while now. An if statement within my FixedUpdate calls gets called at random times when it doesn’t even meet the conditions.

This is the statement:

if (hit.collider != null && hit.collider.tag == “bug” && Input.GetTouch (0).phase == TouchPhase.Began && hitcheck == true && Main.GameOver == false) {

							if (hit.transform.gameObject.renderer.enabled == true) {
									Main.TotalApples = Main.TotalApples + 1;
									Main.ApplesEarned = Main.ApplesEarned + 1;
							}
							bugishit = true;
							StopCoroutine (Currency ());

							StartCoroutine (Currency ());
							

							hitcheck = false;

							
					}

This is the full method:

	void FixedUpdate ()
	{

			if (Input.touchCount >= 1) {

					hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint ((Input.GetTouch (0).position)), Vector2.up);

					//On touch
					if (hit.collider != null && hit.collider.tag == "bug" && Input.GetTouch (0).phase == TouchPhase.Began && hitcheck == true && Main.GameOver == false) {
							
							
							if (hit.transform.gameObject.renderer.enabled == true) {
									Main.TotalApples = Main.TotalApples + 1;
									Main.ApplesEarned = Main.ApplesEarned + 1;
							}
							bugishit = true;
							StopCoroutine (Currency ());

							StartCoroutine (Currency ());
							

							hitcheck = false;

							
					}

					if (Input.GetTouch (0).phase == TouchPhase.Ended && hitcheck == false) {
							hitcheck = true;
					}
			}

	}

I notice when I play the game that even if I don’t hit or tap on a bug, the prior if statement still gets called. And it does this at random times. Any help is greatly appreciated! Thanks :slight_smile:

**EDIT: YES! I found the problem, but I am at a total loss on how to fix it.
My raycast points up to infinity, so even if i tap under a bug it collides with it.

hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint ((Input.GetTouch (0).position)), Vector2.up);

anybody know any way for fixing this? Thanks in advance =D**

Have you tried Debug.Log()ing the variables involved? The if will only fire if all conditions are met (I’ve been programming for over a decade and I’ve never seen anything else). So, that must mean that one of your conditions is not what you expect.

I’d say, whenever it fires, Debug.Log all the variables it checks and see which one has an unexpected value. Once you know that, you should be able to search for all references to that variable and figure out where it’s getting it’s erroneous value.