Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unity Coin system bug

Discussion in 'Getting Started' started by Yegor42069, Mar 27, 2023.

  1. Yegor42069

    Yegor42069

    Joined:
    Dec 5, 2022
    Posts:
    26
    Im making a 2d platformer and i Just added a coin system but everytime my player goes to the next level the coin count starts counting up by two instead of one and the coin count restarts Im not sure why and I need help

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class CoinPicker : MonoBehaviour
    7. {
    8.     private int Coins = 0;
    9.     public TextMeshProUGUI textcoins;
    10.  
    11.     private void OnTriggerEnter2D(Collider2D other) {
    12.         if (other.gameObject.CompareTag("Coins")) {
    13.             Coins = Coins + 1;
    14.             textcoins.text = Coins.ToString();
    15.  
    16.  
    17.             Destroy(other.gameObject);
    18.         }
    19.     }
    20.  
    21. }  
    22.  
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    3,416
    Are there maybe 2 colliders?
     
  3. Yegor42069

    Yegor42069

    Joined:
    Dec 5, 2022
    Posts:
    26
    No I just checked none of the coins on either levels has two colliders
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    3,416
    Then throw debug logs in it. You can add the other gameobject as second value and then if you don't destroy it you can ping it when you click on the log
     
  5. Yegor42069

    Yegor42069

    Joined:
    Dec 5, 2022
    Posts:
    26