- Home /
How do I stop a timer when a sphere gets to a certain point?
Basically I have set up a random grid using Primm's Algorithm. When the game is started the grid is randomly generated with a start point (highlighted green) which is always at (0,0,0) and a randomly generated end point (highlighted red). I have placed a sphere on the start point for the user to navigate the grid/maze, and I have also created a timer that begins to count as soon as the game is loaded, but I want this timer to stop whenever the sphere hits the end point.
The timer I use is one I found on a tutorial site (don't think I'm allowd to link to this site), but it doesn't come with a stop clause. Is there any way in which to do this?
Hope this has been clear enough, Thanks :)
This is the code that I am currently using for the timer:
private var startTime : float;
var textTime : String;
function Start() {
startTime = Time.time;
}
function OnGUI () {
var guiTime = Time.time - startTime;
var $$anonymous$$utes : int = guiTime / 60; //This creates the $$anonymous$$utes
var seconds : int = guiTime %60; //This creates the seconds
var fraction : int = (guiTime * 100) % 100; //$$anonymous$$iliseconds, obviously.
textTime = String.Format ("{0:00}:{1:00}:{2:00}", $$anonymous$$utes, seconds, fraction); //The way it is displayed on screen
GetComponent(GUIText).text = textTime;
}
Same example as I provided below can be applied here. Create a new bool
, which will be true
while you want the timer to run, and false
when you want the timer to stop.
Then wrap the line
var guiTime = Time.time - startTime;
within an if()
using the new bool as the conditional. This way, the timer only increments if the bool is true
.
Write an additional method to change the value of this bool
to true
or false
and call these methods when and however you like. In your particular situation, you said you wanted it to stop when your sphere reaches the end point. Like I mentioned in the answer below, use a collider and OnTriggerEnter()
to achieve this.
Try and get as much working as you can on your own. Search the Scripting Reference and the Component Reference to get more help, and also watch these tutorial videos that explain the use of colliders:
http://unity3d.com/learn/tutorials/modules/beginner/physics/colliders
http://unity3d.com/learn/tutorials/modules/beginner/physics/colliders-as-triggers
http://unity3d.com/learn/tutorials/modules/beginner/physics/on-collision-enter
If you have more questions, feel free to ask a new question with more specific details.
Hi thanks for the quick reply! Unfortunately when I try and apply your code to my GUIText the timer is not displayed on the screen. I tried to incorporate some of the elements into the code I already have, but I feel really out of my depth working in such a way.
At the $$anonymous$$ute I have your code attached to the GUIText and as I already said it's not displaying the timer. Can you help me any further?
Thanks again for even replying :)
Here's the code right now: #pragma strict
private var currentTime = 0;
private var running : boolean;
function Start()
{
running = true;
}
function Update()
{
if (running)
{
currentTime += Time.deltaTime;
}
}
function StopTimer()
{
running = false;
}
function StartTimer()
{
running = true;
}
function Reset()
{
running = true;
currentTime= 0;
}
Answer by guitarxe · Dec 16, 2013 at 07:20 PM
Without seeing the code, we cannot really help with whatever script you are using. If you have gotten that code from some site that prohibits you from re-distributing it, then try asking for support on that site.
Otherwise, consider writing your own. A very simple timer can be done like this:
public class Timer : MonoBehaviour
{
private static float currentTime = 0;
private static bool running;
void Start()
{
running = true;
}
void Update()
{
if (running)
{
currentTime += Time.deltaTime;
}
}
public static void StopTimer()
{
running = false;
}
public static void StartTimer()
{
running = true;
}
public static void Reset()
{
running = true;
currentTime = 0;
}
}
To detect your sphere object entering the end point, the simplest method would be to place a collider there and set it to be a trigger in the inspector. Then you write additional code to disable the timer when the sphere enters the trigger, utilizing the OnTriggerEnter()
function.
Your answer
![](https://koobas.hobune.stream/wayback/20220613124310im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Timed particle system disable 0 Answers
Timer that stops at the end of the game 3 Answers
RotateAround x degrees over y time 1 Answer
Check for closed circuit of pickups 2 Answers