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);
}
}
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!";
}
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220612161746im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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