Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by admiralmattbar · Apr 30, 2018 at 05:08 AM · scorelevelpoints

Adding score in if statement within Update() happens continuously.

Hello,

I am making a game where a player navigates a maze and gets points after completing the maze. The old maze should leave the game and the new maze replace it and the score based on how long it took to navigate the maze appears. The problem is the score keeps adding and the new level never comes in. Here is the code in my Update() method in my GameController class.

 void Update () {
 
         GameObject currentLevel = GameObject.FindGameObjectWithTag ("clonelevel");
         if (currentLevel != null) {
             xPosLev = currentLevel.GetComponent<Rigidbody> ().transform.position.x;
             //Debug.Log ("xPosLev is " + xPosLev.ToString());
         }
 
         if (BallMover.GetTouching () == true) { //Player touches right answer
             enableScoreAdding = true;
             winPoints = 10;
             EndLevel ();
             mathProblem.text = "CORRECT!";
         }
 
         if (BallMover.GetTouchingWrong() == true) { //Player touches wrong answer
             winPoints = 0;
             mathProblem.text = "WRONG!";
             EndLevel ();
         }
 
     }

I tried to stop the score addition by creating a variable enableScoreAdding so that when the score gets added in a loop it then, after once, sets the variable to false. This has not worked. Here is the method where the score is added and called (in the EndLevel method).

 public int CalcScore()
     {
         Debug.Log ("Adding 10 to Score");
         if (winPoints > 0) { 
             scoreBonus = (int)((100 / Timer.GetTime ())) * (Timer.GetTime ());
         } else {
             scoreBonus = 0;
         }
         return winPoints + scoreBonus;
     }

This method makes the level fly out and it is destroyed when it contacts an offscreen gameobject.

 public void EndLevel()
     {
         //changingLevel = true;
         GameObject thisPlayer = GameObject.FindGameObjectWithTag ("Temp Player");
         Destroy (thisPlayer);
         GameObject thisLevel = GameObject.FindGameObjectWithTag ("clonelevel");
         if (enableScoreAdding == true) {
             score += CalcScore ();  //Increase score
         }
         enableScoreAdding = false;  //Stop score adding
         scoreDisp.text = score.ToString (); //Display score to screen
 
 
         if (thisLevel != null) {
             Rigidbody rb = thisLevel.GetComponent<Rigidbody> ();
 
             answer1.text = "";
             answer2.text = "";
             answer3.text = "";
             answer4.text = "";
 
             a1.enabled = false;
             a2.enabled = false;
             a3.enabled = false;
             a4.enabled = false;
 
             rb.isKinematic = false;
             rb.AddForce (transform.right * 100f);
         }
 
     }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by admiralmattbar · May 02, 2018 at 05:28 PM

Hi, Thanks for the reply. What was happening is the player was entering the collision box but then disappeared and not exiting so the result was always true and the game got stuck in there. I just added an additional condition requiring the player to not be null.

 if (BallMover.GetTouching () == true && playerClone != null) { //Player touches right answer and exists.
              enableScoreAdding = true;
              winPoints = 10;
              EndLevel ();
              mathProblem.text = "CORRECT!";
          }
Comment
Add comment · Show 1 · 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
avatar image DawdleDev · May 02, 2018 at 06:32 PM 1
Share

Glad you fixed the problem!

avatar image
1

Answer by DawdleDev · Apr 30, 2018 at 02:16 PM

First, move enableScoreAdding into the if statement that checks if enableScoreAdding is true. Second, try printing the value of enableScoreAdding to the console to see if it isn't getting used correctly. Third, make sure that its value isn't getting set anywhere else (it could be getting constantly set to true somewhere), and that it isn't a local variable (it's at the top of the script, not in the EndLevel function. Hope this helps!

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

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help With Score C# Error CS0131 1 Answer

For Loops Situation 1 Answer

Highscore with Dreamlo 3 Answers

How do I add extra points for coming close to obstacle but NOT for coming close and colliding? 1 Answer

spawn object every 10 score points 1 Answer


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