- Home /
How can I add to an existing variable only once?
Hello all. I have an inventory of darts in my game that starts out as 50. When "round 2" begins, I want to add 50 more darts to whatever total is leftover from "round 1" for example: if I have 10 leftover from round 1, I want to have 60 to start round 2. My code now reads: if round == 2, darts.dartvalue +=50. the problem is that it updates my inventory for like 4 seconds while "round 2" is being displayed and I end up with 5000 darts. How can I just add 50 once to my overall total? Thank you for helping!
Well it's obvious that you put your code in the update loop, this is your problem. $$anonymous$$ove it to a function and call it once, or if round 2 is in a new scene, move it to OnEnable or Start().
Otherwise you can make some janky system like this....
public int DartCount
{
get
{
return _dartCount;
}
set
{
if (round > lastRound)
{
round = lastRound;
_dartCount = value;
}
}
}
private int _dartCount;
private int lastRound = 1;
Why not do a method called ResetRound()
that will add the darts to players?
Answer by NoMoneys · Mar 26, 2018 at 04:56 PM
You could check if the 50 points have been added with a boolean variable that turns to false when the 50 darts have been added. Like this:
bool readyToAddPoints = true;
if(readyToAddPoints)
{
darts.dartsvalue += 50;
readyToAddPoints = false;
}
Then whenever you have to update the darts value again, just set the bool to true.
Hey thank you, that worked. I just had to put it inside a function then call that function and it works perfectly.
This solution only works if you only have 2 rounds or aren't resetting your readyToAddPoints bool.
having a hard time resetting it b/c the rounds are all in the same scene
Don't forget about my getter/setter solution, that accommodates exactly your situation.
Answer by EvanCheddar · Mar 26, 2018 at 08:49 PM
The cleanest/easiest solution I came up with was to call the static variable via an animation event that happens only if you make it to the next round therefore calling a function that adds more darts to the player inventory. Thank you all for your help, to anyone looking at this in the future, there are some real solutions here. All the best, take care!
Sorry to tell you but that is not a clean way to do it. But if it works for you... :|
seems pretty easy/clean, what would be considered a cleaner solution?
There is nothing clean in using static to fix a problem that should be fixed in a different way.
I would recommend to set states to your Game. You are talking about rounds, so you can easily create a Game class with the different states of it Prepare, Reset, Start, Pause, Resume, Over so you can manipulate data from the class and not from an animation. You will be working on this and in some weeks you are going to be screa$$anonymous$$g who the hell is giving darts to the players :P
Yeah you might be right, the game is fairly simple so hopefully I can get by this way. It's my first game so I'm learning a lot. Im already towards the end of development so I just have to make do with the some what sloppy code right now. For my next game, I'll be much neater and organized that's for sure.
Then you go! Good luck with your development! :D
Hey guys come on, it's just a unity animation event on some basic game, who cares?
Your answer
![](https://koobas.hobune.stream/wayback/20220612155414im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Function is not updating the variable 1 Answer
How to get in sync two scripts if the Update () calls are random? 2 Answers
Variable goes very high in update method 1 Answer
Accessing variables from GameObjects in an array 3 Answers
Calling a mesh instance 'mesh' or 'msh' (to avoid the keyword) 1 Answer