My score gets incremented a lot of times

Hello! I have been working on a 2d game and I implemented a score system for some reasons my total score is way more For Example: My last total score was 7 and I just got a score of 2, The total score becomes 9 And if you want to see the script here it is

public class CoinCollecter : MonoBehaviour
{
public TMP_Text score_TXT;
public TMP_Text highScore;
int score = 0;
public AudioSource audioSource;
public AudioClip coinSFX;
public TMP_Text totalScore;

// Start is called before the first frame update
void Start()
{
    totalScore.text = PlayerPrefs.GetInt("totalscore").ToString();
    Time.timeScale = 1f;
    GetComponent<BoxCollider2D>().enabled = true;
    score = PlayerPrefs.GetInt("score", 0);
    highScore.text = PlayerPrefs.GetInt("Score", score).ToString();
    score_TXT.text = score.ToString();
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.tag == "Player")
    {
        if (PlayerPrefs.GetInt("score") == 9999)
        {
            PlayerPrefs.SetInt("levelReached", 2);
        }

        if (2 > PlayerPrefs.GetInt("levelReached", 1))
        {
            PlayerPrefs.SetInt("levelReached", 2);
        }
        audioSource.PlayOneShot(coinSFX);
        score = PlayerPrefs.GetInt("score", 0) + 1;
        GetComponent<BoxCollider2D>().enabled = false;
        PlayerPrefs.SetInt("score", score);
        PlayerPrefs.SetInt("totalscore", PlayerPrefs.GetInt("totalscore", 0) + PlayerPrefs.GetInt("score"));
        totalScore.text = PlayerPrefs.GetInt("totalscore").ToString();
        score_TXT.text = score.ToString();
        if (score > PlayerPrefs.GetInt("Score", 0))
        {
            PlayerPrefs.SetInt("Score", score);
            score_TXT.text = score.ToString();
            highScore.text = score.ToString();
        }
        Destroy(gameObject);
    }
}

}

And one more thing if I collect the coin 1 time and lose the game it shows the right total score but if I collect the coin 2 times it shows 3 point.

Hi! Sorry but I’m slightly confused about the question. You said that “some reasons my total score is way more For Example: My last total score was 7 and I just got a score of 2, The total score becomes 9”

I assume that this is the intended scoring system and not the problem.

You said that collecting the coin 2 times will show 3 points which I assume is the totalscore. From what I can tell, this is from collecting the coin 1 time which turns score to be 1 and adds that to the totalscore.

score = PlayerPrefs.GetInt("score", 0) + 1;       (line 27)

PlayerPrefs.SetInt("totalscore", PlayerPrefs.GetInt("totalscore", 0) + PlayerPrefs.GetInt("score"));   (line 30)

The second time you collect the coin, the score turns into 2 (because 1 is added onto the previous 1) and is added to the totalscore (1) which turns the totalscore to 3. Remember that Playerprefs saves the same data throughout until you reset it.
I assume you want the totalscore to be 2 after the second coin so you can either just set the totalscore to be the score since the score is pretty much doing totalscore’s job.

PlayerPrefs.SetInt("totalscore", score);       (replace line 30)

You could also just remove the totalscore variable and set the totalscore text to score, depending on your needs. Hope this helps.