Problems with count++?

Hello gentlemen well I have some troubles counting an event:

var eventCount: int = 0;

if(Player.transform.position.y < - 5){

eventCount++;

}

If my player falls to -5, my variable eventCount its suposed to go up to 1 but it goes to 2.

Any ideas on how to solve that?

3 Answers

3

Add a timeout before it can add again?

Sorry if I sounded a little sarcastic. It's a genuine suggestion though. Also, you're not providing information about how/when the position is set to be higher then -5 again, so I can't say if this happens in a single frame or not.

It is a little difficult to ascertain what you want from that section of code.

Unless you are processing the event instantly the eventCount will increment every frame that your transform position is less than -5... that could be 1000 times a second. So as Joshua answered, adding a timeout before you can increment eventCount again will probably fix your issue.

Also I don't know if it was a typo in your question or an unnoticed error in your code but:

var eventount: int = 0;

should be

var eventCount: int = 0;

It is just an unoticed error but that doesnt matter, this is just the name of the variable.