# Problem with falling platform script

**URL:** https://discussions.unity.com/t/problem-with-falling-platform-script/500164
**Category:** Scripting
**Tags:** Scripting
**Created:** [March 19, 2013, 9:45am UTC](https://discussions.unity.com/t/problem-with-falling-platform-script/500164 "2013-03-19T09:45:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![doubleJ](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@doubleJ](https://discussions.unity.com/u/doubleJ)
#### Post date: [March 19, 2013, 9:45am UTC](https://discussions.unity.com/t/problem-with-falling-platform-script/500164/1 "2013-03-19T09:45:20Z")

</div>

Greetings community and please help me.

I am stuck with one problem in my script. Basically script should make platform fall after 3 seconds as soon as it collided with a specified game object.

Here is my script:

```auto
var fallTime: float = 3.0; //Interval to fall
private var fall: float;//Time to fall
private var canFall: boolean;
var object: GameObject;
private var rb: Rigidbody;//Object rigidbody;

function Start(){
	canFall = false;	
	fall = 0.0;
}

function OnTriggerEnter(col: Collider){
	if(col.gameObject.tag == "EarthSpirit"){
		Debug.Log("PlatformWillFall");
		canFall = true;
		rb = object.gameObject.AddComponent(Rigidbody);
		rb.useGravity = false;
	}
}

function Update(){
	Debug.Log(Time.time);	
	if(canFall Time.time > fall){
		fall = Time.time + fallTime;
		rb.useGravity = true;
	}
}

```

The problem is that as soon as game object collides with a platform, it falls down with no delay. I think problem is in **Time.time** , because it is calculating from the moment scene starts.

Thanks for any help in advance.

---

<div class="post-metadata">

### Author: ![LeftyRighty](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@LeftyRighty](https://discussions.unity.com/u/LeftyRighty)
#### Post date: [March 19, 2013, 9:58am UTC](https://discussions.unity.com/t/problem-with-falling-platform-script/500164/2 "2013-03-19T09:58:59Z")

</div>

ok, you need a “start” time when the collision happens, and the update should be checking for TimeNow \> StartTime + Delay

so somewhere in the OnTriggerEnter function you need something like

```auto
startTime = Time.time;

```

and the if in the Update function should look something like

```auto
if(Time.time > startTime + delay)
{
...

```

---

<div class="post-metadata">

### Author: ![doubleJ](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@doubleJ](https://discussions.unity.com/u/doubleJ)
#### Post date: [March 19, 2013, 10:29am UTC](https://discussions.unity.com/t/problem-with-falling-platform-script/500164/3 "2013-03-19T10:29:04Z")

</div>

Hmm… Tried your solution and it is still not working…

startTime = Time.time still calculates time no matter what ( and yeah i inserted it in OnTriggerEnter function ). I logged its value and it shows that as soon as game object reaches trigger and collision occurs, value of startTime variable already over 3 seconds.

Any ideas?

---

<div class="post-metadata">

### Author: ![LeftyRighty](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@LeftyRighty](https://discussions.unity.com/u/LeftyRighty)
#### Post date: [March 19, 2013, 10:41am UTC](https://discussions.unity.com/t/problem-with-falling-platform-script/500164/4 "2013-03-19T10:41:43Z")

</div>

yes, Time.time starts when you hit play…

Time.time at start 0  
collision happens at Time.time = 10 → store the Time.time into StartTime var  
in each update, check if Time.time \> StartTime (ie 10) + Delay (ie 3)  
when Time.time \> 13 the contents of the if block happens.

actually there’s an example of a delay on Time.time reference page: [Unity - Scripting API: Time.time](http://docs.unity3d.com/Documentation/ScriptReference/Time-time.html)

---

<div class="post-metadata">

### Author: ![doubleJ](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@doubleJ](https://discussions.unity.com/u/doubleJ)
#### Post date: [March 19, 2013, 11:01am UTC](https://discussions.unity.com/t/problem-with-falling-platform-script/500164/5 "2013-03-19T11:01:02Z")

</div>

Oh yeah, that’s right… Thanks my friend
