Double tap to reduce score by 2 points
Hello, I have a problem with the score system I have. So i have an object, when a user double taps the screen the object teleports to that location but it costs the user 2 points of their score, so if the user has 0 points then they cannot use the double tap feature unless their score is greater than or equal to 2.
I have a scoreScript class which displays the score in the UI:
// This script contains the txt and value of the score
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreScript : MonoBehaviour {
public static int scoreValue = 0;
Text score;
// Use this for initialization
void Start () {
// Making a reference to our score text game object.
score = GetComponent<Text>();
} // Start
// Update is called once per frame
void Update () {
score.text = "Score: " + scoreValue;
} // Update
} // Class ScoreScript
And this code under is the double tap feature located in the update method of another class containing movement controls. This should subtract 2 points if double tap is detected and the score is greater than 1:
// DOUBLE TOUCH
// for each touch in array touches if tap count detected is 2
// teleport mainBody to that location
// costs player 2 points of his score
foreach (Touch touch in Input.touches)
{
if (touch.tapCount == 2 && ScoreScript.scoreValue > 1)
{
// reduce points by 2
// POINTS NOT SUBTRACTING CORRECTLY?!?!?
ScoreScript.scoreValue-= 2;
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
touchPosition.z = 0f;
transform.position = touchPosition;
} // if
} // for
The problem I have is that the score does not get subtracted by 2 correctly, if I have 29 points and I double tap it suddenly reduces the score to a random number such as 13 and does not decrease by 2 (27 should be the expected result) or if my score is in the single digits such as 9 then it will be reduced to 0 or 1 if I use the double tap feature. I have looked around for a solution but cannot find one, how can something like this be fixed?
Thank you
NOTE:
ScoreScript.scoreValue-= 2;
That line is being called multiple times within the for loop when the double tap feature is used and also when I remove the for loop leaving the If statement only, that same line is still being called multiple times, how can i stop this?
First of all, you may wanna add a break
command in your double tap loop, just to be sure. Secondly, can you check (e.g. via some on-screen debug message) how often the subtraction code line is called (e.g. you may accidentally have the script attached to multiple gameObjects).
Hi, so I have put I have used`Debug.log()` to see how many times the subtraction line is called and it is called multiple times randomly. I also removed the foreach
loop leaving the if statement and for some reason the subtraction line is still called multiple times. I have checked where the script is attached to and can confirm its only attached to one game object
Your answer
Follow this Question
Related Questions
How to save highscores 1 Answer
I have no Idea why my highscore Script isn't working ... 0 Answers
switch between 5 Ui icons by pressing a button 0 Answers
Score Counter 0 Answers
Shooting an instantiated missile the way a object is facing. 0 Answers