- Home /
Int increased twice and not only once
Hi, I have a scoreboard in my simple football game, and i want to increase team's score by 1 when the ball passes the goal line. But when it goes in the cage, the score increases by 2, like 0-0 to 2-0...
Here's my Update method :
void Update ()
{
lol = transform.position.x;
int caca = Team1Score-1;
if (lol <= -84.4)
{
Team2Score++;
resetBall();
}
else if (lol >= 84)
{
Team1Score = (Team1Score +1) + Team1Score - (caca);
resetBall();
}
else
{
Team1Score = Team1Score -1;
}
text.text = Team1Score + " - " + Team2Score;
}
If i don't put the second and third else, the score increments to the infinite.. Someone knows why ? Thanks for your help.
What does resetBall()
do ? And why are you incrementing $$anonymous$$m1 score with such a funky formula ?
Team1Score = (Team1Score +1) + Team1Score - (caca);
Answer by maccabbe · Mar 08, 2015 at 08:19 PM
Your score is increasing by 2 due to arithmetic
int caca = Team1Score-1; (line 5)
Team1Score = (Team1Score +1) + Team1Score - (caca); (line 15)
can be simplified to
Team1Score = (Team1Score +1) + Team1Score - (Team1Score-1);
Team1Score = (Team1Score +1) + Team1Score - Team1Score+1;
Team1Score = (Team1Score +1)+1;
Team1Score = Team1Score +2;
I don't see why you don't just get rid of line 5 and replace line 15 with
Team1Score = Team1Score +1;
but if you just want fix your current problem you need to change line 15 to
Team1Score = 2*Team1Score - (caca);
Your answer
![](https://koobas.hobune.stream/wayback/20220613185426im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
controlling the increment in the variable 1 Answer
detect if a variable has stop increasing 4 Answers
Increasing or decreasing int values quickly? 1 Answer
How to increase two floats for every frame the space button is pushed down? 1 Answer
How To Add A Simple Speed Boost On Collision With A Specific Object With A Character Controller 0 Answers