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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Adding points by time and by pickups

Discussion in 'Scripting' started by Potatosaurus, Nov 11, 2019.

  1. Potatosaurus

    Potatosaurus

    Joined:
    Apr 25, 2018
    Posts:
    2
    I'm giving 10 points to the player each second with Score.Update() and also have pickups which triggers Coin.OnTriggerEnter2D() when collided with and executes Score.AddScore() to add another amount of points to the player. The problem is that, while the coins do add points to the player, those points disappear when the next tick of time-points executes and the points go back to what they would have been if i hadn't picked up the coin. I've been trying to understand this but i think i'm too inexperienced to find this problem.

    Thanks for any help!

    Here are my two classes (Coin is attached to the pickup gameobjects):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Score : MonoBehaviour
    7. {
    8.     public Text scoreText;
    9.  
    10.     private float timer;
    11.     public int score;
    12.  
    13.     void Start()
    14.     {
    15.         score = 0;
    16.         scoreText.text = "Score: " + score;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         timer += Time.deltaTime;
    22.  
    23.         if (timer > 1f)
    24.         {
    25.             AddScore(10);
    26.             timer = 0;
    27.         }
    28.     }
    29.  
    30.     public void AddScore(int addedValue)
    31.     {
    32.         score += addedValue;
    33.         scoreText.text = "Score: " + score;
    34.     }
    35. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Coin : Score
    7. {
    8.     private int value = 25;
    9.     private Rigidbody2D rb;
    10.  
    11.     void Start()
    12.     {
    13.         scoreText = GameObject.Find("ScoreText").GetComponent<Text>();
    14.         rb = this.GetComponent<Rigidbody2D>();
    15.     }
    16.  
    17.     private void OnTriggerEnter2D(Collider2D other)
    18.     {
    19.         if (other.tag == "Player"){
    20.             AddScore(value);
    21.             Destroy(this.gameObject);
    22.         }
    23.     }
    24. }
     
  2. Antofourn

    Antofourn

    Joined:
    Nov 26, 2017
    Posts:
    1
    Your score variable should be a public static int, and your update method should only run on Score class. In your example, because Coin class is derived from Score, Coin was also running the base Update();


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Score : MonoBehaviour
    8. {
    9.     public Text scoreText;
    10.  
    11.     private float timer;
    12.     public static int score;
    13.  
    14.     void Start()
    15.     {
    16.         score = 0;
    17.         scoreText.text = "Score: " + score;
    18.     }
    19.  
    20.     public virtual void Update()
    21.     {
    22.         timer += Time.deltaTime;
    23.  
    24.         if (timer > 1f)
    25.         {
    26.             AddScore(10);
    27.             timer = 0;
    28.         }
    29.     }
    30.  
    31.     public void AddScore(int addedValue)
    32.     {
    33.         score += addedValue;
    34.         scoreText.text = "Score: " + score;
    35.     }
    36. }
    37. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Coin : Score
    7. {
    8.     private int value = 25;
    9.     private Rigidbody2D rb;
    10.  
    11.     void Start()
    12.     {
    13.         scoreText = GameObject.Find("ScoreText").GetComponent<Text>();
    14.         rb = this.GetComponent<Rigidbody2D>();
    15.     }
    16.  
    17.     private void OnTriggerEnter2D(Collider2D other)
    18.     {
    19.         if (other.tag == "Player")
    20.         {
    21.             AddScore(value);
    22.             Destroy(this.gameObject);
    23.         }
    24.     }
    25.  
    26.     public override void Update()
    27.     {
    28.      
    29.     }
    30. }
     
  3. Potatosaurus

    Potatosaurus

    Joined:
    Apr 25, 2018
    Posts:
    2
    Ah, forgetting to set score to static was a mistake, but i didn't know about the Update thing. Figured i just remove it from Coin just to not clutter it but i didn't realize it would then run Score.Update() instead. Thanks, all solved!