Hello everyone, I am having a little issue with displaying the score on scree. When a game object is destroyed the player gets 10 points for each. The script I am currently using does not increment the score by 10 point. It only shows once and does not increment. Please help, I am stuck with this. Thanks in advance. Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.UI; public class Bricks : MonoBehaviour { private int count; public Text countText; // Use this for initialization void Start () { count = 0; SetCountText (); } // Update is called once per frame void Update () { } void OnCollisionEnter(Collision col){ Destroy (gameObject); count = count + 10; SetCountText (); } void SetCountText (){ countText.text = "Score :" + count.ToString (); } }
you are destroying this gameobject and all things attached to it, this script included. The score must be held and displayed on another gameobject that isn't destroyed (usually something like the GameManager script etc.). This script then tells that script "add some points" before destroying itself.
It looks like your count variable is on each brick. Each brick starts with count 0. Gets destroyed and sets the count to 10 and sets your score string to ten. You need a score variable that sits in another class and only has on instance. You could create a score manager class to handle this. In your brick On collision function call this manager class and increment the score there.