In-game counter stops counting when game is built?
Thanks for reading my message. In my game, I have a counter set up which displays the number of checkpoints in my level, and when the player reaches each checkpoint, the counter deducts 1 from the count. this works perfectly in the editor as well as when the game is built but once you die and the level is restarted, then the counter will never count down. What is confusing me is that this is only a problem once the game is built, not in the editor (which runs exactly how I anticipated). any help will be appreciated! The script below is being used by other scripts, but literally just to enable/disable it (for instance, when the player dies, I use "counter.enabled = false" to hide the counter, then the player can press the restart button, then the counter is shown upon the reloaded level if that makes sense)
Here is the counter script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Count : MonoBehaviour
{
public Text counterText;
public int numberOfCheckpoints;
void Start ()
{
SetCountText ();
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Checkpoint"))
{
numberOfCheckpoints = numberOfCheckpoints - 1;
SetCountText ();
}
}
void SetCountText ()
{
counterText.text = "Checkpoints: " + numberOfCheckpoints.ToString ();
}
}
Answer by alankemp · Jul 24, 2017 at 08:00 PM
Do you ever set numberOfCheckpoints back to its starting value when the player presses the restart button?
You could add another member variable for the maximum number of checkpoints and set numberOfCheckpoints equal to that when the level starts.
If you are enabling/disabling the counter when the game starts and ends you could use the OnEnable() function instead of Start():
public class Count : MonoBehaviour
{
public Text counterText;
public int numberOfCheckpoints;
public int maxNumberOfCheckpoints;
void OnEnable()
{
numberOfCheckpoints = maxNumberOfCheckpoints;
SetCountText ();
}
Your answer
Follow this Question
Related Questions
Creating multiple text components on a canvas 1 Answer
i cant put text into a ui text thru a c# script 0 Answers
Help! Unity does not include all of my UI elements in the apk file! [SOLVED] 1 Answer
a ton of cs0246 errors on scripts that arent even mine, that happened out of nowhere 0 Answers
Canvas Renderer event 0 Answers