Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by TiJack · Mar 08, 2015 at 08:07 PM · intincreasetwice

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.

Comment
Add comment · Show 1
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 NoseKills · Mar 08, 2015 at 08:04 PM 0
Share

What does resetBall() do ? And why are you incrementing $$anonymous$$m1 score with such a funky formula ?

 Team1Score = (Team1Score +1) + Team1Score - (caca);
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
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

3 People are following this question.

avatar image avatar image avatar image

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


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