Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nsingh13 · Jun 12, 2015 at 07:52 PM · raycastbugmobileif-statementsfixedupdate

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**

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nsingh13 · Jun 12, 2015 at 07:59 PM 0
Share

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.

avatar image Nsingh13 · Jun 12, 2015 at 08:07 PM 0
Share

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

avatar image FortisVenaliter · Jun 12, 2015 at 08:11 PM 1
Share

Yeah, look at the other overrides for that Raycast function. One of them takes in a float for maximum distance to check.

avatar image Nsingh13 · Jun 12, 2015 at 08:17 PM 0
Share

Thanks again! It's fixed now :-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges