Search Unity

Issue trying to add score

Discussion in 'Scripting' started by munsi211, Feb 18, 2020.

  1. munsi211

    munsi211

    Joined:
    Jan 28, 2020
    Posts:
    2
    I'm relatively new to using Unity, and I'm making a 2D infinite runner, where you have to get a high score by surviving as long as you can. I'm trying to set up a script that adds the score at a constant rate and attach this code to a string that should update the score, but the string remains unchanged. The string uses TextMesh Pro and the String itself says "Score: 0". I'm hoping to find a way to have that 0 constantly increase over time.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class PointsManager : MonoBehaviour
    7. {
    8.     float PlayerScore;
    9.     public TextMeshProUGUI text;
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         PlayerScore += Time.deltaTime;
    14.     }
    15.  
    16.     public void DisplayScore()
    17.     {
    18.  
    19.         text.text = "Score: " + PlayerScore.ToString();
    20.     }
    21. }
    22.  
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    put text.text inside update
     
  3. munsi211

    munsi211

    Joined:
    Jan 28, 2020
    Posts:
    2
    Thank you so much it worked!!
     
  4. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    no problem