- Home /
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 :)
**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**
Answer by FortisVenaliter · Jun 12, 2015 at 07:56 PM
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.
bangs head on table. Wow thanks, I should have tried that a long time ago. I'll try and let you know what I find. It's really funny you answered though, becuase I was just reading one of your other answers on a different question lol.
YES! I found the problem, but I am at a total loss on how to fix it. $$anonymous$$y 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);
do you know any way for fixing this? Thanks in advance =D
Yeah, look at the other overrides for that Raycast function. One of them takes in a float for maximum distance to check.
Your answer
Follow this Question
Related Questions
2D raycast on mobile is giving weird results 0 Answers
Why isn't hit.transform.name with ray working? 1 Answer
Multiple raycasts in same fixedupdate 3 Answers
OnMouseDown() ios subsitute not function correctly 0 Answers
Jump Force (rigid body velocity) is not equal in different mobile phones 0 Answers