Alright, so I have been trying to develop a racing game for a few days now. My timer (connected to a GUIText) starts from zero and then counts up forever.
var timerText : GUIText;
var startTime : float = 0;
var timer : float;
function Start(){
timerText = GameObject.Find("clock").GetComponent(GUIText);
}
function Update () {
timer = startTime + Time.time;
timer += Time.deltaTime;
timerText.text = FormatTime (timer);
}
function FormatTime (time : float){
var minutes : int = Mathf.Floor(time / 60.0);
var seconds : int = Mathf.Floor(time - minutes * 60.0);
var milliseconds = time - Mathf.Floor(time);
milliseconds = Mathf.Floor(milliseconds * 1000.0);
var sMinutes = "00" + minutes.ToString();
sMinutes = sMinutes.Substring(sMinutes.Length-2);
var sSeconds = "00" + seconds.ToString();
sSeconds = sSeconds.Substring(sSeconds.Length-2);
var sMilliseconds = "000" + milliseconds.ToString();
sMilliseconds = sMilliseconds.Substring(sMilliseconds.Length-3);
timeText = sMinutes + ":" + sSeconds + ":" + sMilliseconds;
return timeText;
}
What I want to do is change the displayed (running) time either plus 10 seconds or minus 10 seconds depending on a collision. I have come up with a collision script that I think should be working and connected it to my car.
var timer : float;
var startTime : float = 0;
function OnTriggerEnter( other : Collider) {
if(other.tag == "plus") { //Make A tag that is "AddTime"
Debug.Log("hit");
timer += 10; //Change to how much the time it adds
}
if(other.tag == "minus") { //Make A tag that is "subtractTime"
timer += -10; //Change to how much the time it adds
}
}
It works, like collisions occur and it adds or subtracts time in the console but no results are actually displayed on the timer. Someone please help!
You’re creating a separate variable timer on the object with the OnTrigger functions. That timer is in no way related to the one you have that is dealing with the GUI timer. What you want is to store the gameobject with the GUI timer script then increase or decrease the timer using a function. Also, your timer is increasing infinitely because you placed it in Update. So if your game ends or is paused, it will continue to add. You can stop it from updating by adding a simple if-statement.
Alright, thanks! So in order to affect the timer in my timer script, would it be a getComponent or is there some other way?
You can store the variable after using GetComponent in Start so you don’t have to call it again.
var myObj;
function Start()
{
myObj = GameObject.Find("timerObject").GetComponent(TimerScript);
}
Alright, so I stored the variable and called it with the GetComponent function.
function Start(){
timer = GameObject.Find("clock").GetComponent(time);
}
function OnTriggerEnter( other : Collider) {
if(other.tag == "plus") { //Make A tag that is "AddTime"
Debug.Log("hit");
timer += 10; //Change to how much the time it adds
}
if(other.tag == "minus") { //Make A tag that is "subtractTime"
timer += -10; //Change to how much the time it adds
}
}
There is now an error in line 12 stating that the identifier timer is unknown.
You need to use a function inside your Time script to increase or decrease the timer.
function OnTriggerEnter(other : Collider)
{
if(other.tag == "plus")
{
timer.AdjustTime(10);
}
else if(other.tag =="minus")
{
timer.AdjustTime(-10);
}
}
So I don’t need two separate scripts at all?
When I add the above to the time.js I get an error that I have never seen before…
A quick Google search tells me that you’re getting that error due to an empty JS file.
Also, what you have in mind is a very bad idea. You’ll end up creating several instances of timer if you have multiple objects.
Thanks. I should of utilized my resources…
New code:
var timerText : GUIText;
var startTime : float = 0;
var timer : float;
function Start(){
timerText = GameObject.Find("clock").GetComponent(GUIText);
}
function Update () {
timer = startTime + Time.time;
timer += Time.deltaTime;
timerText.text = FormatTime (timer);
}
function FormatTime (time : float){
var minutes : int = Mathf.Floor(time / 60.0);
var seconds : int = Mathf.Floor(time - minutes * 60.0);
var milliseconds = time - Mathf.Floor(time);
milliseconds = Mathf.Floor(milliseconds * 1000.0);
var sMinutes = "00" + minutes.ToString();
sMinutes = sMinutes.Substring(sMinutes.Length-2);
var sSeconds = "00" + seconds.ToString();
sSeconds = sSeconds.Substring(sSeconds.Length-2);
var sMilliseconds = "000" + milliseconds.ToString();
sMilliseconds = sMilliseconds.Substring(sMilliseconds.Length-3);
timeText = sMinutes + ":" + sSeconds + ":" + sMilliseconds;
return timeText;
}
function OnTriggerEnter(other : Collider)
{
if(other.tag == "plus")
{
timer.AdjustTime(10);
}
else if(other.tag == "minus")
{
timer.AdjustTime(-10);
}
}
There are no visible changes taking place with the timer though collisions are happening.
First of all…You didn’t fully read what I said.
Secondly, you defined your timer variable as a float. I meant for you to create a timer script to deal all the time and display and stuff. Then you get it with GetComponent and store it into a variable so you can call a function to adjust the timer.
Thirdly, you were suppose to define the AdjustTime function by yourself inside your timer class outside of your script dealing with the triggers.