Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Igor_Vasiak · Nov 27, 2017 at 09:40 PM · scripting problembasicintegeroperator

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.

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

2 Replies

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

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.

Comment
Add comment · Show 11 · 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 Igor_Vasiak · Nov 27, 2017 at 10:05 PM 0
Share

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?

avatar image MaxGuernseyIII Igor_Vasiak · Nov 27, 2017 at 10:12 PM 0
Share

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.

avatar image Igor_Vasiak MaxGuernseyIII · Nov 27, 2017 at 10:39 PM 0
Share

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.

Show more comments
Show more comments
avatar image shadowpuppet Igor_Vasiak · Nov 27, 2017 at 10:15 PM 0
Share

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

}

avatar image Igor_Vasiak shadowpuppet · Nov 27, 2017 at 10:34 PM 0
Share

I'm using FixedUpdate. It's a physics deal. Thanks for the tip, anyway. :D

avatar image
1

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);
  }
Comment
Add comment · 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

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

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

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


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