I’m trying to make a game where you have to pick up the jewels and avoid the enemies. Each time I go to the next level, the score and time resets. Please help.
public Text timerText;//Link to this variable by dragging your UI textbox on the variable
public float timeLeft;//this is the variable that will hold how much time is left
//Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> (); //this is so we can adjust the component
count = 0;
moveSpeed = 0.05f;
spriteRenderer = GetComponent<SpriteRenderer> ();
CountText.text = "score: " + count;
timeLeft = 0f;//set to 10 seconds
}
//Update is called once per frame
void Update () {
//take a small amount of time off the variable everytime we enter the fram
timeLeft += Time.deltaTime;
//display time left
timerText.text = "Time: " + (Mathf.RoundToInt (timeLeft)) .ToString ();
}
void OnCollisionEnter2D(Collision2D other) {
if (other.gameObject.CompareTag ("Jewel")) { //this object will disappear
other.gameObject.SetActive (false);
count = count + 1;
CountText.text = "Score: " + count;
}
if (other.gameObject.CompareTag ("Timer")) {
other.gameObject.SetActive (false); //this object will disappear
timeLeft -= 5f;
if (timeLeft <= 0f) {
timeLeft = 0f;
}