How do I add/subtract time upon collision?

Alright, I just made a timer that starts counting up on start up. What I need is every time the racer hits an object, it adds or subtracts time from the timer depending on the object. How do I come by this? I assume it is an OnCollisonEnter or OnTriggerEnter function but I am lost from there. Please help, I am quite new at this! :slight_smile:

    var timerText:GUIText;

    var startTime:float = 0;
    
    function Update () {

        timerText = gameObject.GetComponent(GUIText);

        timer = startTime + Time.time;
      
        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;

    }
timeOverall = timeOverall + timeMarker;

here hope this helps

//Make sure you make the “Object” that makes your time, add OR subtract to make sure that it is set to “IsTigger”
//When I was learning unity3d I was told that ONE of the Object that you are going to enter HAS to have a Rigid body Im am not sure why
//Make sure you have the tag set to the “Object”…
//Hope this helps :smile:
//If not email me - logang730@gmail.com -
function OnTiggerEnter( other : Collider) {

if(other.tag == “AddTime”) { //Make A tag that is “AddTime”

startTime += 10; //Change to how much the time it adds

}
if(other.tag == “subtractTime”) { //Make A tag that is “subtractTime”

startTime += -10; //Change to how much the time it adds

}
}

Well what you’re going to want to do is find a way to differentiate your different objects. Like adding tags to them. Then you can use an OnCollisionEnter and check to see what it needs to do from there like

function OnCollisionEnter(collision : Collision)
{
if(collision.transform.tag == "SomeTag")
DoThis();
if(collision.transform.tag == "OtherTag")
DoSomethingElse();
}

I’m not sure if I solved you’re problem, if I didn’t feel free to Pm me on the forums :smile:

So I have the timer script above connected to a GUIText called “clock”. Does the collision text need to be separate and connected to the car or the objects? Also, if it is supposed to be separate, how do I manipulate the GUIText so that it shows the additions or subtractions? Thanks for the swift responses guys!

I have no idea where to add this. Care to elaborate for a newb’s sake :slight_smile:

In onCollisionEnter or OnTriggerEnter, the action, that takes place when you want to increase or decrease time.

You have your time value, of overall. Then add to it the time bonus or penalty.

Would I not have to define timeOverall somewhere in the script?

or would it simply look like this

funcition OnTriggerEnter(collision : Collision){
if(collision.gameObject.name == "name"){

timeOverall = timeOverall + timeMarker;
}
}

No, any variable must be defined. So yes, you would have to define either locally, in the function, or globally at the top of script.

This may be a very random question but is this by any chance part of a school assignment?