- Home /
how to stop a timer but not the whole sense when you collect cubes
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class playercontroller : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
public Text timerText;
public float startTime;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
setCountText();
winText.text = "";
startTime = Time.time;
}
void Update()
{
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
setCountText();
}
}
void setCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 12)
{
winText.text = "You Win!";
Time.timeScale = 0;
}
}
}
Answer by ryanjvigus · Oct 05, 2020 at 08:47 PM
Maybe you could create a private bool that starts true and you set to false once the last cube is collected. Then just make it so that the stuff that updates the timer in Update () only runs if the bool is true.
Something like:
private bool timerEnabled = true;
void Update ()
{
if (timerEnabled)
{
//timer update stuff
}
}
void setCountText ()
{
if (count >= 12)
{
wintext.text = "You Win!";
timerEnabled = false;
}
}
Your answer
Follow this Question
Related Questions
How to make a stopwatch that restarts every time the player dies and saves it as a highscore 3 Answers
How to make timer for music in my android app(20,30 and 60 min)? in #c 0 Answers
Else if not getting executed,Else if statement not executing 1 Answer
help with 3d scroller skateboard chase script 0 Answers
Issue with destroying tiles 1 Answer