A
A
Artem2019-06-24 16:48:38
C++ / C#
Artem, 2019-06-24 16:48:38

How to stop the timer when a character dies, uinty?

How to stop the timer when a character dies?
Here is the C# code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{

    public Text scoreText;

    void Start () {
    InvokeRepeating("RunTimer", 1, 1);
  }
 
  void RunTimer() {
    scoreText.text = (int.Parse(scoreText.text) + 1).ToString();
  }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2019-06-24
@GavriKos

Implement the timer not through InvokeRepeating, but through something more controlled - Update, for example, or a coroutine.
Specifically in your case:

void RunTimer() {
if (IsAlive())
    scoreText.text = (int.Parse(scoreText.text) + 1).ToString();
  }

Implement IsAlive according to your internal logic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question