- Home /
Counter issues, hitting -1?
Hello there all,
I've been having this problem with my counter, although I have wrote :
if(units <=0)
{
units = 0;
}
if(units >=99)
{
units = 99;
}
When manipulating it through code, it hits -1, then goes to 0. That quick hit is causing havock with my numbers, adding and extra unit where there should not be one.
Can someone give me a hand please?
Many thanks,
Keep it up guys!
Well, a bit more information is needed on what is increasing or decreasing these numbers.
I'm looking at that and thinking that somehow too many "units" get removed, but it is some time before the code above runs, and corrects the mistake.
I would also be interested to know why you can't stop it beco$$anonymous$$g -1 in the first place. When you are subtracting one, shouldn't you first check that there are more than zero units?
Is it possible to get those if statements directly after your decrementing statements? It's hard to tell what's going on with just your if statements.
But it seems there is something in between your decrementing and the correction. Can you verify?
thanks for the quick response!
2nd time ive wrote this, did not post the first....
Its hard for me to explain, Im building an inventory in where you can craft items together. So I roam the world collecting materials, for instance "wood", I get it from an object by using:
inventory_Items.wood++;.
Then I go to my Inventory screen, where when I press on the wood button and I get an image of wood and a + & - next to it, so that I can add wood in quantities. So when I hit the + the units in the hud script go up, and the wood goes down in the inventory_Items script, thats all good, but when I press - it goes down to 0, then if I press again, it hits -1 then 0, but it gives me one of wood when doing it.
I am very greatfull fro all your anwsers, and sorry not expressing myself very clearly.
$$anonymous$$ale - I'm sorry I did not quite get your anwser, thanks anyway.
dangermouse - I do check to see if there is more than zero and Im doubble checking all my scripts to see, but it looks ok...Thanks a bunch also.
Erich5h5 - when I read mathclamp I was leaping for joy! I thought, thats it!, and know although its not visibly going under zero(stays at zero), if for instance I had 1 wood but presssed the - button twice (going below zero once, althought its not visible), and then kept pressed the + again, I would have 2 wood...
Thanks yet again all,
Patience is indeed a virtue,
Cheers!
Can you give us the exact code that runs when you press the - button in your inventory? That would really help. and if that runs any other functions, possibly that too.
What you are doing sounds right, it should work, if the code makes sense.
Answer by Eric5h5 · Nov 17, 2012 at 02:34 AM
There's nothing in the code you posted that would cause any issues, but:
units = Mathf.Clamp (units, 0, 99);
Your answer
Follow this Question
Related Questions
How to get RenderTexture non-square? 2 Answers
door script problems 2 Answers
Variable Mathf.Clamp not working... (SOLVED) 1 Answer
Is anyone able to fix my code? 3 Answers
What's the difference between clamp and repeat textures? 2 Answers