- Home /
How to add time to a timer?
I have a timer javascript that countsdown and turns off a camera when it hits 0. Now I need to make another script that adds time to the timer when you pick up an object (A battery in this case) so that the camera lasts longer.
This is my timer script:
var timer : float = 30.0;
var sound: AudioClip;
var camera1 : Camera;
var camera2 : Camera;
var CameraSwitch : CameraSwitch;
function Awake(){
CameraSwitch = GameObject.Find("camHolder").GetComponent("CameraSwitch");
}
function Update()
{
timer -= Time.deltaTime;
if(timer <= 0)
{
timer = 0;
audio.PlayOneShot(sound);
camera1.gameObject.SetActive(true);
camera2.gameObject.SetActive(false);
CameraSwitch.Playsound();
}
}
function OnGUI()
{
GUI.Label(new Rect(155, 100, 75, 23), "Battery: " + timer.ToString("0"));
}
Answer by wijesijp · May 27, 2014 at 10:27 AM
Following code is in C# but I think you will get the idea...
First add a function to your main class (one you have posted) to increase the time
public void IncreaseCamTime(float tm)
{
timer += tm;
}
Now at the place you find the battery, you need to call your main class (one you have posted) and tell it to increase time. Using gameobject find you can get hold of your main class and call "IncreaseCamTime" function you have added
Another way is to send a message to the class
Sorry, but I don't know anything about C# I'm pretty new to the whole game design scene.
javascript function should be ...
function IncreaseCamTime(float tm)
{
timer += tm;
}
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Typing a value, not a var, syntax in js? 2 Answers
Get integer or float value of a variable 2 Answers
How to get the key from Array value 1 Answer
Unexpected token? 1 Answer