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 Dogg · Dec 21, 2014 at 03:53 AM · c#float

Float not working all the time?

Hello, it's been a while. My question is, why is it that my float that increases every time on collision with the Player, sometimes not starts a coroutine to end the game? Take a look at the script:

 public float clickCount = 0f;
 public CircleCollider2D collider1;
 public BoxCollider2D collider2;
 public CircleCollider2D collider3;
 
 void Update()
 {
 if(clickCount == 100){
             Die = true;
             collider1.enabled = false;
             collider2.enabled = false;
             collider3.enabled = false;
             StartCoroutine(End());
             Debug.Log("Victory");
 }
 }
 
 IEnumerator End()
     {
         yield return new WaitForSeconds (5.0f);
         Application.LoadLevel("MainTestScene2");
     }
 
 void OnCollisionEnter2D (Collision2D col)
     {
         if (col.gameObject.tag == "Player") {
             clickCount++;
         }
     }
 }

Basically after the Player collides with the game object with this script, the level is supposed to end. It works for the most part, but sometimes it doesn't and I continue to collide with the game object forever. What could be the problem here? I was thinking it might be because it says only 100, and maybe I collide with the game object too fast so it passes 100 and doesn't start coroutine. If that's the case, then how can I make it say if(clickCount == to or > 100)?

Comment
Add comment · Show 4
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 meat5000 ♦ · Dec 21, 2014 at 04:09 AM 0
Share
 if(clickCount >= 100f)

Don't forget the f. Its important in C#.

Also, I suggest making it an int as you are using Integers and so floating point is a bit redundant.

Put a Debug.Log("SO$$anonymous$$ETHING"); before your yield in the Coroutine.

avatar image Eric5h5 · Dec 21, 2014 at 04:14 AM 1
Share

Well, adding an f makes no practical difference here. Changing == to >= helps somewhat, but could still have an incorrect result if clickCount ended up being 99.999999. The real solution is not to use floats at all. Honestly I don't think it even makes sense that the ++ operator works on floats.

avatar image meat5000 ♦ · Dec 21, 2014 at 04:23 AM 0
Share

Ha, I never even noticed that one (++ on float).

Well 2 out of 3 aint bad

Also, I suggest making it an int

avatar image Owen-Reynolds · Dec 21, 2014 at 07:11 AM 1
Share

If you've never seen >=, then there's a ton of other basic stuff you've never seen as well. Think you should get any "Learn C# in 10 easy steps" book and skim it.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Dec 21, 2014 at 04:08 AM

Don't attempt to directly compare floats, due to floating point imprecision. Your clickCount variable should be an int. Always use ints where possible, unless you really need floating point.

Comment
Add comment · Show 6 · 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 meat5000 ♦ · Dec 21, 2014 at 04:24 AM 0
Share

...in C# ;)

avatar image Eric5h5 · Dec 21, 2014 at 05:48 AM 2
Share

? In any language.

avatar image Owen-Reynolds · Dec 21, 2014 at 07:21 AM 1
Share

Sure, but doubt that's the problem. Float addition doesn't have rounding errors because of cheap parts. It only rounds on repeating fractions, so adds 1's and 1/2's perfectly.

The things that fool people are that 0.1 is a repeating fraction in binary. So 0.1+0.1 ... 10 times appears to have rounding error from 1, for no reason.

It's probably triggering two collisions and double-jumping from 99 to 101.

avatar image meat5000 ♦ · Dec 21, 2014 at 02:05 PM 0
Share

Always use ints where possible

@Eric5h5

I am under the impression that in uJS, int doesn't really exist and so each time you use it, it's a parsed float. Am I wrong?

Pedantry, really, but in a tight system where cycles count, it's a consideration, right? (Even if a little off topic from Question) :)

avatar image Eric5h5 · Dec 21, 2014 at 05:20 PM 1
Share

@meat5000: yes, you're wrong. Of course int exists; since Unity uses $$anonymous$$ono, Unityscript uses the same types as all other .NET languages. Unityscript runs at the same speed as C# since it's all converted to the same bytecode in the end.

Show more comments

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

New to Unity, Need Help. 1 Answer

Accelerometer Quick Question(Moving Up and Down?) 1 Answer

How To Add PlayerPrefs Scores? 1 Answer

Facebook SDK and ADColony Compatible in iOS? 2 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