How can I update timer using a collision?
I have a empty game object called Clock with this script attached, I want to try and add time when I collid into another game object.
var timer : float = 10.0; //Create varable Timer of type float
function Update ()
{
timer -= Time.deltaTime;//Varable time decrements from set value (10.0)
if(timer <= 0)//When timer = 0
{
timer = 0;
Application.LoadLevel(6);//loads the game over scene
}
}
function OnGUI ()
{
GameObject.Find("TimerText").GetComponent.<UI.Text>().text="Timer:" + timer.ToString("0");
}
My Second script is on the game object I want to collid with once I hit this it should update the timer but I cant figure out where Im going wrong
import UnityEngine.UI;
function OnTriggerEnter (info:Collider)//when trigger enters
{
var pickUpAudio : AudioSource = GetComponent.<AudioSource>();
pickUpAudio.Play ();
//Timer.timer +=10;/
transform.position = Vector3(0,-1000,0);
yield WaitForSeconds (1.5);
Destroy (gameObject);/
}
What results are you getting with your code? Does Timer.timer += 10 work? Does it add time but not in the way you want to? Do you want to reset the timer with colliding or just add a portion of time to it?
I'm trying to add time by 10seconds each time theres a collision. it doesnt add time but the countdown is working fine. Im confused :(
You need to get a reference to your Timer
script component and then access timer
variable to add time to it.
Hi Harshadk, what do you mean by reference my Timer? Is Timer.timer +=10; a refernce to it? Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220612081035im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make one object pass through one object but hit another? 2 Answers
Destroy Object on Collision (with di sound) 0 Answers
How to raycast object as far as possible, without collision? 0 Answers
Multiplayer 2 Answers
How to change bullet on impact? 3 Answers