Hello everyone,
I have tried to no avail today to try and implement a timer counting down when object gets instantiated. (Well the timer IS implanted and it DOES count down) BUT the problem is the timer follows the game’s time since it first loaded and not when the object instantiates. I am sure the answer is right under my nose and I just can’t see it. So i am reaching out to the community on how to reset this game timer or is there another variable I am missing.
The code has been borrowed from the forums (Thanks community for sharing!) and adapted some of it for my script that does the actual function of the timer and displays it. If you need other scripts I have the button that calls the function to instantiate the prefab and the script that does the actual function.
And it would need to reset each time the player instantiates a new object. (every instantiated object has an independant timer if possible)
var target : Transform; // Object that this label should follow
var offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
var clampToScreen = false; // If true, label will be visible even if object is off screen
var clampBorderSize = .05; // How much viewport space to leave at the borders when a label is being clamped
var useMainCamera = true; // Use the camera tagged MainCamera
var cameraToUse : Camera; // Only use this if useMainCamera is false
var startTime = 300.0;
static var startTimer : boolean = false; //starts Timer when plant is instantiated
//private var gameCharacter : Transform;
//private var distance : float;
private var cam : Camera;
private var thisTransform : Transform;
private var camTransform : Transform;
function Start () {
thisTransform = transform;
if (useMainCamera)
cam = Camera.main;
else
cam = cameraToUse;
camTransform = cam.transform;
//gameCharacter = GameObject.Find("First Person Controller").GetComponent(Transform);
}
function Update () {
if (clampToScreen) {
var relativePosition = camTransform.InverseTransformPoint(target.position);
relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
thisTransform.position.z);
}
else {
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
}
if (startTimer) // timer starting when first plant is instantiated
{
Time.time = 0;
timeTaken = startTime - Time.time;
guiText.text = FormatTime (timeTaken);
}
}
function FormatTime (time)
{
var intTime : int = time;
var minutes : int = intTime / 60;
var seconds : int = intTime % 60;
//var fraction : int = time * 10;
// fraction = fraction % 10;
//Build string with format
// 17[minutes]:21[seconds]:05[fraction]
// timeText = minutes.ToString () ;
//timeText = timeText + seconds.ToString ();
timeText = String.Format ("{0:00} {1:00}", minutes, seconds);
// timeText += fraction.ToString ();
return timeText;
}
@script RequireComponent(GUIText)
thanks for the help guys and gals,
LW
If you use (alt ie)time.deltaTime and set it inside a bool condition then it wont start to countdown till you want it to.
function Start() {
timerName = 300
}
//just example code
function Update() {
if(startTimer==true) {
timerName -= 1*Time.deltaTime;
}
if(timerName>=0)(timerName<=1) {
startTimer = false;
timerName = 0;
}
}
Just some example Hope it helps 
It works!!! Thanks so much Black Mantis. This has driven me insane at work. (Yes i do unity3d at work when im not getting any calls)
Since it’s counting down it stops at 00:00 which is perfect.
I’m actually getting the hang of this. Thanks again! I owe you one. 
LW
Hi again,
I have the script almost working. Now when the timer reaches 0 on the first instantiated object, the script stops the timer on all the instantiated objects.
var target : Transform; // Object that this label should follow
var offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
var clampToScreen = false; // If true, label will be visible even if object is off screen
var clampBorderSize = .05; // How much viewport space to leave at the borders when a label is being clamped
var useMainCamera = true; // Use the camera tagged MainCamera
var cameraToUse : Camera; // Only use this if useMainCamera is false
static var startTimer : boolean = false; //starts Timer when plant is instantiated
var timerName = 300.0;
private var cam : Camera;
private var thisTransform : Transform;
private var camTransform : Transform;
private var gameCharacter : Transform;
private var distance : float;
function Start () {
thisTransform = transform;
if (useMainCamera)
cam = Camera.main;
else
cam = cameraToUse;
camTransform = cam.transform;
gameCharacter = GameObject.Find("First Person Controller").GetComponent(Transform);
}
function Update () {
distance = Mathf.Sqrt((gameCharacter.position - transform.position).sqrMagnitude);
if (clampToScreen) {
var relativePosition = camTransform.InverseTransformPoint(target.position);
relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
thisTransform.position.z);
}
else {
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
}
// TIMER FOR PLANTING
if (startTimer == true) // timer starting when first plant is instantiated
{
timerName -= 1*Time.deltaTime;
guiText.text = FormatTime (timerName);
}
if (timerName >= -1 timerName <= 1)
{
timerName = 0;
startTimer = false;
}
if (timerName > 1)
{
startTimer = true;
}
//timeTaken = startTime - Time.time;
//startTimer = false;
}
function OnMouseEnter() {
if(distance <= 4.0) {
guiText.text = FormatTime (timerName);
}
}
function FormatTime (time)
{
var intTime : int = time;
var minutes : int = intTime / 60;
var seconds : int = intTime % 60;
//var fraction : int = time * 10;
// fraction = fraction % 10;
//Build string with format
// 17[minutes]:21[seconds]:05[fraction]
// timeText = minutes.ToString () ;
//timeText = timeText + seconds.ToString ();
timeText = String.Format ("{0:00} {1:00}", minutes, seconds);
// timeText += fraction.ToString ();
return timeText;
}
@script RequireComponent(GUIText)
Hi,
This is because your variable startTimer is static :
static var startTimer : boolean = false;
So when you modify it, it is modified everywhere.
If i change the variable, the other script that triggers the boolean gives me an error.
An instance of type ‘objectlabel’ is required to access non static member ‘startTimer’.
Here is the script trying to access it.
When the gui button is pressed, it calls this script to instantiate the object, it triggers the boolean to true so that the timer is hanging above the object and starts counting down.
//caroutine variable for each plant development
var yieldseconds : float = 5.0;
var yieldseconds2 : float = 5.0;
// plant variables
var plant1p1 : Transform;
var plant2p1 : Transform;
var plant3p1: Transform;
var plant4p1 : Transform;
var plant5p1 : Transform;
var plant6p1 : Transform;
var plant7p1 : Transform;
var plant8p1 : Transform;
var plant9p1 : Transform;
var plant10p1 : Transform;
var plant11p1 : Transform;
var plant12p1 : Transform;
// Plant experience variables
var plantexperience1 = 2;
function planting1()
{
var clone : Transform;
var clone2 : Transform;
var clone3 : Transform;
var clone4 : Transform;
var clone5 : Transform;
var clone6 : Transform;
var clone7 : Transform;
var clone8 : Transform;
var clone9 : Transform;
var clone10 : Transform;
var clone11 : Transform;
var clone12 : Transform;
var hit : RaycastHit;
var ground = transform.TransformDirection(-Vector3.up);
if(Physics.Raycast(transform.position, ground, hit, 0.1))
{
//hitpass = hit.normal;
playerlevel.currentXP = playerlevel.currentXP + plantexperience1;
if (playerlevel.currentXP == playerlevel.nextlevelUP)
{
playerlevel.initiallevel++;
playerlevel.nextlevelUP = playerlevel.nextlevelUP * 2;
}
objectlabel.startTimer = true;
clone = Instantiate(plant1p1, transform.position, Quaternion.identity);
yield WaitForSeconds (yieldseconds);
clone2 = Instantiate(plant2p1, clone.transform.position, clone.transform.rotation);
Destroy(clone.gameObject);
yield WaitForSeconds (yieldseconds);
clone3 = Instantiate(plant3p1, clone2.transform.position, clone2.transform.rotation);
Destroy(clone2.gameObject);
yield WaitForSeconds (yieldseconds);
clone4 = Instantiate(plant4p1, clone3.transform.position, clone3.transform.rotation);
Destroy(clone3.gameObject);
yield WaitForSeconds (yieldseconds);
clone5 = Instantiate(plant5p1, clone4.transform.position, clone4.transform.rotation);
Destroy(clone4.gameObject);
yield WaitForSeconds (yieldseconds);
clone6 = Instantiate(plant6p1, clone5.transform.position, clone5.transform.rotation);
Destroy(clone5.gameObject);
yield WaitForSeconds (yieldseconds);
clone7 = Instantiate(plant7p1, clone6.transform.position, clone6.transform.rotation);
Destroy(clone6.gameObject);
yield WaitForSeconds (yieldseconds);
clone8 = Instantiate(plant8p1, clone7.transform.position, clone7.transform.rotation);
Destroy(clone7.gameObject);
yield WaitForSeconds (yieldseconds);
clone9 = Instantiate(plant9p1, clone8.transform.position, clone8.transform.rotation);
Destroy(clone8.gameObject);
yield WaitForSeconds (yieldseconds);
clone10 = Instantiate(plant10p1, clone9.transform.position, clone9.transform.rotation);
Destroy(clone9.gameObject);
yield WaitForSeconds (yieldseconds);
clone11 = Instantiate(plant11p1, clone10.transform.position, clone10.transform.rotation);
Destroy(clone10.gameObject);
yield WaitForSeconds (yieldseconds);
clone12 = Instantiate(plant12p1, clone11.transform.position, clone11.transform.rotation);
Destroy(clone11.gameObject);
}
return;
}
LW
I could be wrong in my reading of your script and understanding of what you are trying to achieve. But if you just need the timer to start when the object is instantiated. Remove the variable call from the other script and in your function Start () just turn it on:
pseudo code:
var startTimer : boolean = false;
[...]
function Start(){
[...]
startTimer = true;
[...]
}
Guess I was trying to be too fancy. Thanks for the Start() tip that was what was missing. 
I love this community. <3
LW