- Home /
Timer Countdown problem
good evening I would like to make a countdown timer of, and in my onCollision I incremented by 20 seconds. I have done this but it did not work:
if(hit.gameObject.tag == "HouseCollider"){
//increment timer
timer+=60;
UpdateComponents("time");
}
And on my UpdateComponents :
if(component == "time"){
GameObject.Find("time").guiText.text=""+timer;
}
I have a 3DText to display my timer : function OnGUI () {
//60 seconds for begining tests (timer = 60)
var guiTime = timer - (Time.time - startTime);
var minutes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;
textTime = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction);
GameObject.Find("time").guiText.text=""+textTimer
And i don't know why timer didn't increment by 20 seconds .
long explanation of INvokeRepeating() etc at http://unityGE$$anonymous$$S.com. hope it helps!
I'm sorry that's +60 seconds i have changed it... It's on my first code ...
@Fattie : thanks but it should increment the timer OnTrigerEnter, like this :
function OnTriggerEnter(hit : Collider) {
if(hit.gameObject.tag == "HouseCollider"){ if(gifts > 0){ //increment timer timer+=60; ... }
And it doesen't work ..
check in Debug.Log, if your trigger works right or not... then in 3dText ongui put code where u assign timer from string (text) like: timer = int.TryParse(GameObject.Find("time").guiText.text); ??
And don't forget to check, what is variable timer and how you acces it in first snippet. is it from other script or not.. I can't imagine whole idea how it should work.. going look to forum at unityGE$$anonymous$$S.com
The main thing. Can u post some snippet to make me happy =)))
if i understood u well, i go make my implementation of this thing
Answer by XienDev · Nov 04, 2012 at 06:44 PM
why First String is textTime in third snipped, and second is textTimer. may be it is your mistake ?
Answer by XienDev · Nov 04, 2012 at 06:56 PM
Ok. I've made my variant in c#.. here is screen
My Text3dScript.cs
using UnityEngine;
using System.Collections;
public class Text3dScript : MonoBehaviour
{
float startTime;
TextMesh textMesh;
public static float timer = 60;
void Start()
{
startTime = Time.time;
textMesh = this.gameObject.GetComponent<TextMesh>();
}
// Update is called once per frame
void Update () {
float guiTime = timer - (Time.time - startTime);
if (guiTime > 0)
{
int minutes = (int)(guiTime / 60);
int seconds = (int)(guiTime % 60);
int fraction = (int)((guiTime * 100) % 100);
textMesh.text = string.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction);
}
}
}
my TriggerScript is
using UnityEngine;
using System.Collections;
public class triggerScript : MonoBehaviour {
void OnTriggerEnter(Collider c)
{
Text3dScript.timer += 20;
}
}
How can you see. All is ok, and works
Your answer
Follow this Question
Related Questions
Timer not work :( 0 Answers
For loop resetting itself, but needs to stop 2 Answers
Countdown timer into text c# 1 Answer
How to restart a level with countdown? 4 Answers
Is an object with an update function possible? (C#) 1 Answer