- Home /
script countdown
Hello!
I need some help. In this script countdown, in my game is activated by triggers and also always disabled with trigger. everything works fine, but when reactivated through triggers the sript countdown will not start again from 60 seconds, but always remains zero. How do I update this script so that it always start again from 60 seconds every time the script reactivated? thank you!
My current script.
public var currentTime : int = 60;
var style : GUIStyle;
function Start()
{
InvokeRepeating( "CountDown", 1, 1 );
}
function CountDown()
{
if (--currentTime == 0)
{
Debug.Log("Time is up my friend");
CancelInvoke( "CountDown" );
}
}
function OnGUI ()
{
GUILayout.Label(currentTime.ToString(),style);
}
I don't understand what you are trying to do. Do you want the time to count down from 60 when a collider touches the gameobject's collider, which is set to IsTrigger?
Have you tried placing currentTime = 60; in the Start()?
Hello! In short I would like the countdown when turning off the script itself through this other script below, you must return to 60 rather than to zero on the variable, so as to divide by 60 seconds when the script is reactivated.
All this must operate during active play. Thank you.
Script that toggles through triggers the script (Tempos).
Script Name (enable disable script).
var Player : GameObject;
var Script : Tempos;
function OnTriggerEnter(other : Collider) //Check if something has entered the trigger ( and declares this object in "other" )
{
if(other.collider.tag == Player.tag) //Checks if the Player is inside the trigger
{
Script = GetComponent(Tempos);
Script.enabled = true; //enables theScript.
}
}
function OnTriggerExit(other : Collider) //Check if something has entered the trigger ( and declares this object in "other" )
{
if(other.collider.tag == Player.tag) //Checks if the Player is inside the trigger
{
Script = GetComponent(Tempos);
Script.enabled = false; //disable theScript.
}
}
Script Name countdown (Tempos).
public var currentTime : int = 60;
var style : GUIStyle;
function Start(){
InvokeRepeating( "CountDown", 1, 1 );
}
function CountDown()
{
if (--currentTime == 0)
{
Debug.Log("Time is up my friend");
CancelInvoke( "CountDown" );
}
}
function OnGUI () {
GUILayout.Label(currentTime.ToString(),style);
}
Answer by Pecek · Oct 14, 2013 at 12:37 AM
You have to set the timer manually every time and it will work. On trigger enter - call the countdown script, and set the value.
Answer by meat5000 · Oct 14, 2013 at 04:50 PM
For performing an operation on script disable, try
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnDisable.html
I do not understand where this code must be entered. I have to create a new script? I need to add it in one of the two?
OnDisable function () { print ("script was removed"); }
Place it at the end of your script. It will be called when the script is disabled.
It's similar to OnDestroy()
Your answer
Follow this Question
Related Questions
Activate script when player enters trigger zone. 2 Answers
How to stop a Countdown Timer? 1 Answer
activate script from trigger 1 Answer
Enable/Disable script with timer 1 Answer
Play parent animation without affecting the child animation 1 Answer