- Home /
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)?
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.
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.
Ha, I never even noticed that one (++ on float).
Well 2 out of 3 aint bad
Also, I suggest making it an int
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.
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.
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.
Always use ints where possible
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) :)
@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.
Your answer
Follow this Question
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