Integer++; not working as expected - randomly raising by two, instead of one.
if (Input.GetKeyDown(KeyCode.W) && jumpAmount < StatsProp.PlStats[PlayerStats.MaxJumpAmount])
{
rbody.velocity = new Vector2(rbody.velocity.x, 8 / Time.timeScale);
jumpAmount += 1;
print("JA: " + jumpAmount);
}
This is the piece of code that doesn't work properly.
I'm working on a 2D Survival - Adventure - Space Shooter - Action game (Don't Die, or Project S.A.S.S.A.). On the simplest things I seem to fail.
The script should do this:
I press the W key.
The player jumps.
jumpAmount increases by 1.
It prints jumpAmount.
The player jumps just fine, but if I increase the max amount of jumps, let's say, to 5, sometimes when I jump jumpAmount int increases by 2, or even 3 instead of 1. This reasults on my player jumping less than it should.
Is there an explanation on why is this happening? Is it that my keyboard is being detected twice sometimes, or am I writing it the wrong way?
I know that this is basic, and probabbly really obvious, but thanks in advance for any help.
Answer by MaxGuernseyIII · Nov 27, 2017 at 09:49 PM
The probability that incrementing an integer is not working properly is vanishingly small. It is far more likely that it is being incremented an extra time somewhere else or the code is being called more times than you actually think it is.
You can measure what is actually happening more accurately by logging both the before and after value:
var before = jumpAmount;
jumpAmount += 1;
var after = jumpAmount;
Debug.Log("before = " + before + " and after = " + after);
Assuming you aren't in some weird pocket of the universe where things work differently from everywhere else, I would venture that after will always be before + 1. Whether there is another piece interfering or your code is being invoked too many times can be identified from whether or not before value on one like is always equal to the after value on one of its most-recent predecessor.
That is, if you jump once and get two log lines for it, then the code is being invoked too often. If two jumps show a "gap" in the jumpAmount, some other code is interfering with the value in a way you didn't anticipate.
About being inside a pocket universe - who knows? I may be, or I may be not. :)
But, about the actual issue, I'm getting 1 to 3 lines on Console, and not a gap. Do you have any suggestions?
So you're saying that you cut and pasted the exact code I supplied and you got the exact following debug log line?
before = 1 and after = 3
That is, not something that is like the above code and two lines, one with "1" and one with "3" but precisely the above code and precisely one line that says it jumped from 1 to 3 after a single increment?
That would definitely suggest you are in a pocket universe or, at least, a region of our universe where something behaves very differently.
Do you have any background threads running? I don't find threading to be very well-supported by Unity and try to stick to coroutines to simulate threading.
No, I didn't copie nor paste your code; The thing that appears to me is this:
JA = 1; //First Jump
JA = 2; //Second Jump
JA = 3; //Second Jump Still.
I might be inside a pocket universe... Please, call Spock, so he can help me.
The only "thread" thatI have in background, that is, if you can call it thread, is Unity Profiler.
FOUND THIS ONLINE: "Use LateUpdate function ins$$anonymous$$d of Update
LateUpdate is said to be called just once per frame, so it should fix the problem. However, depending on your project, its side effect could be to change your train of thought when dealing with logic for the same execution order of scripts. Use a flag
You could use a flag for a double check. Example code:
using UnityEngine; using System.Collections;
public class YourClass : $$anonymous$$onoBehaviour { private bool is$$anonymous$$eyPressed = false;
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow) && !is$$anonymous$$eyPressed)
{
is$$anonymous$$eyPressed = true;
// TODO
// your logic here when button pressed
}
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.LeftArrow) && is$$anonymous$$eyPressed)
{
is$$anonymous$$eyPressed = false;
// TODO
// your logic here when button released
}
}
}
I'm using FixedUpdate. It's a physics deal. Thanks for the tip, anyway. :D
Answer by shadowpuppet · Nov 27, 2017 at 09:56 PM
I get that too sometimes. Don't know the "right" answer but sometimes it helps ( for me anyway) to add a bool and make it a condition - if I understand the problem correctly. Add a bool and make it true then back to false after adding the +1. Can't hurt to try.Give it a shot and let me know if it works
if (Input.GetKeyDown(KeyCode.W) && jumpAmount < StatsProp.PlStats[PlayerStats.MaxJumpAmount])
{
rbody.velocity = new Vector2(rbody.velocity.x, 8 / Time.timeScale);
jumpIncrease = true;//added bool up top and set true
if(jumpIncrease == true){//added condition
jumpAmount += 1;
jumpIncrease = false;//turned off to maybe stop jumpAmount adding more than you want on single key stroke
print("JA: " + jumpAmount);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612143457im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate changes position 1 Answer
Issue with using modulus operator with array 1 Answer
How to Save the Current that what load from playerprefs 0 Answers
How do you change UI Text to an int? 4 Answers
Big numbers 2 Answers