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.
  2. Dismiss Notice

Issues with updating score

Discussion in '2D' started by roan05112008, Feb 8, 2020.

  1. roan05112008

    roan05112008

    Joined:
    Sep 5, 2019
    Posts:
    17
    Hi!
    I have some problems updating my score in a game. I have some power-ups, and when the player touches them, he gets to score points. But, instead of getting 1 point, it gets 2, which means it duplicates.
    What is the problem?
    Please, can you help me?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @roan05112008

    "What is the problem?"

    Well, it would be easier to know, if you did actually show and tell what you have done.

    It might be one of many things you have issues with - your code might cause this, or you might have two of some script in one object or several objects overlapping in your scene.

    But you leave forum readers to guess what you have done...
     
  3. roan05112008

    roan05112008

    Joined:
    Sep 5, 2019
    Posts:
    17
    Ok
    Here is my code
    Player Movement
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public CharacterController2D controller;
    8.     public Animator animator;
    9.     public InputMaster control;
    10.  
    11.     public Vector2 movement;
    12.  
    13.     public float runSpeed = 40f;
    14.  
    15.     float horizontalMove = 0f;
    16.     bool jump = false;
    17.     bool crouch = false;
    18.  
    19.     [Header("Stats")]
    20.     public float score;
    21.     public float money;
    22.     public float health; // .1f == 10 points 1f == 100 points
    23.     public bool isDead;
    24.  
    25.     private void Awake()
    26.     {
    27.         control = new InputMaster();
    28.         control.Player.Move.performed += ctx => movement = ctx.ReadValue<Vector2>();
    29.     }
    30.     private void Start()
    31.     {
    32.         score = 0f;
    33.         money = 0f;
    34.         health = 1f;
    35.         isDead = false;
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update () {
    40.  
    41.         horizontalMove = movement.x * runSpeed;
    42.  
    43.         animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
    44.  
    45.         control.Player.Jump.performed += ctx => jump = true;
    46.         control.Player.Jump.performed += ctx => animator.SetBool("IsJumping", true);
    47.  
    48.         if (Input.GetButtonDown("Crouch"))
    49.         {
    50.             crouch = true;
    51.         } else if (Input.GetButtonUp("Crouch"))
    52.         {
    53.             crouch = false;
    54.         }
    55.  
    56.  
    57.         if(health == 0)
    58.         {
    59.             isDead = true;
    60.             Debug.LogError("The player is dead");
    61.         }
    62.     }
    63.  
    64.     public void OnLanding ()
    65.     {
    66.         animator.SetBool("IsJumping", false);
    67.     }
    68.  
    69.     public void OnCrouching (bool isCrouching)
    70.     {
    71.         animator.SetBool("IsCrouching", isCrouching);
    72.     }
    73.  
    74.     void FixedUpdate ()
    75.     {
    76.         // Move our character
    77.         controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    78.         jump = false;
    79.     }
    80.  
    81.     private void OnEnable()
    82.     {
    83.         control.Enable();
    84.     }
    85.  
    86.     private void OnDisable()
    87.     {
    88.         control.Disable();
    89.     }
    90. }
    91.  
    Player Collisions
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class PlayerCollisions : MonoBehaviour
    8. {
    9.     public PlayerMovement playerMovement;
    10.     public PlayerStats playerStats;
    11.  
    12.     public GameObject PickupEffect;
    13.     private void Start()
    14.     {
    15.        
    16.     }
    17.     private void OnCollisionEnter2D(Collision2D collision)
    18.     {
    19.        
    20.     }
    21.  
    22.     private void OnTriggerEnter2D(Collider2D collision)
    23.     {
    24.         if(collision.gameObject.tag == "Gem")
    25.         {
    26.             Destroy(collision.gameObject);
    27.             playerMovement.score++;
    28.             Debug.Log(playerMovement.score);
    29.             playerMovement.money += 5;
    30.             GameObject temporaryPickup = Instantiate(PickupEffect, collision.gameObject.transform.position, collision.gameObject.transform.localRotation);
    31.             Destroy(temporaryPickup, 1f);
    32.             playerStats.UpdateScore(playerMovement.score);
    33.             playerStats.UpdateMoney(playerMovement.money);
    34.         }
    35.  
    36.         if(collision.gameObject.tag == "Cherry")
    37.         {
    38.  
    39.             Destroy(collision.gameObject);
    40.             GameObject temporaryPickup = Instantiate(PickupEffect, collision.gameObject.transform.position, collision.gameObject.transform.localRotation);
    41.             Destroy(temporaryPickup, 1f);
    42.             playerStats.UpdateHealth(playerMovement.health);
    43.             Debug.Log(playerMovement.health);
    44.             if (playerMovement.health <= .7f)
    45.             {
    46.                 playerMovement.health += .3f;
    47.             }
    48.             else if (playerMovement.health == .8f)
    49.             {
    50.                 playerMovement.health += .2f;
    51.             }
    52.             else if (playerMovement.health == .9f)
    53.             {
    54.                 playerMovement.health += .1f;
    55.             }
    56.         }
    57.  
    58.         if(collision.gameObject.tag == "ResetScene")
    59.         {
    60.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    61.         }
    62.     }
    63. }
    64.  
    PlayerStats
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class PlayerStats : MonoBehaviour
    8. {
    9.     public TextMeshProUGUI scoreText;
    10.     public TextMeshProUGUI moneyText;
    11.  
    12.     public PlayerMovement playerMovement;
    13.     public Slider healthBar;
    14.  
    15.     private void Start()
    16.     {
    17.         healthBar.value = playerMovement.health;
    18.     }
    19.     public void UpdateMoney(float money)
    20.     {
    21.         moneyText.text = "$ " + money.ToString();
    22.     }
    23.  
    24.     public void UpdateScore(float score)
    25.     {
    26.         scoreText.text = "Score: " + score.ToString();
    27.     }
    28.  
    29.  
    30.     public void UpdateHealth(float health)
    31.     {
    32.         healthBar.value = health;
    33.     }
    34. }
    35.  
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @roan05112008

    I don't see anything obvious, in your OnTriggerEnter2D you add score once, and code is in trigger enter (which happens once) and you destroy the pickup...

    Maybe check you really don't have accidental "Gem" or "Cherry" tags in any other object, and make sure you don't have duplicate objects (can't see your scene hierarchy).