Search Unity

How to subtract from an int

Discussion in 'Scripting' started by BeZerK1, Sep 30, 2021.

  1. BeZerK1

    BeZerK1

    Joined:
    Jul 22, 2021
    Posts:
    13
    Sticks++ adds +1 each time , which is what I want but sticks-- doesn't -1 each time , why does this happen?For example if int = 50 it subtracts 6 and keep on going . I need it to -1 from the int value each time.

    Untitled.png
     
  2. Wieditleestisgek

    Wieditleestisgek

    Joined:
    Dec 18, 2016
    Posts:
    43
    use sticks -= 1
     
  3. Wieditleestisgek

    Wieditleestisgek

    Joined:
    Dec 18, 2016
    Posts:
    43
    why isnt -- not -1 thou
     
  4. BeZerK1

    BeZerK1

    Joined:
    Jul 22, 2021
    Posts:
    13
    I've used -= 1 already , I get the same result . When Int = 50 and Q is pressed once , it subtracts 26 for some reason . I'm aware it won't stop at 0 but I still don't understand why it doesnt subtract 1 each time.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    try:

    Code (csharp):
    1. void AddSticks() {
    2.     Debug.Log("before adding, sticks was: " + sticks);
    3.     sticks++;
    4.     Debug.Log("after adding, sticks was: " + sticks);    
    5. }
    6.  
    7. void RemoveSticks() {
    8.     Debug.Log("before removing, sticks was: " + sticks);
    9.     sticks--;
    10.     Debug.Log("after removing, sticks was: " + sticks);  
    11. }
    Also, please post your code in Code Tags rather than a screenshot, so we can copy-paste it when helping.
     
    Bunny83 likes this.
  6. Wieditleestisgek

    Wieditleestisgek

    Joined:
    Dec 18, 2016
    Posts:
    43
    Is it a fixed number that gets removed? or random?
    Maybe u are using onbutton/key instead of onbutton/key down?
     
    BeZerK1 likes this.
  7. BeZerK1

    BeZerK1

    Joined:
    Jul 22, 2021
    Posts:
    13
    Thank you for your input.
    I can confirm Onbutton/Key was the issue
     
  8. BeZerK1

    BeZerK1

    Joined:
    Jul 22, 2021
    Posts:
    13
    Thanks for the input and advice , I'll keep that in mind next time . The issue has been resolved
     
  9. Sleekoy

    Sleekoy

    Joined:
    Sep 17, 2021
    Posts:
    1
    Thanks for this btw, I had a issue with my health bar script, this resolved it.
     
    Yoreki likes this.