<p>I'm not sure what the <code>valueAtTime</code> function is trying to achieve. That takes the value of the property at the given time. In the script you wrote it would sample the value of the opacity at a point a random number of seconds in the past, some time between the current time and maxOffset seconds ago. Unless the opacity changes during that time it won't do anything at all.</p>

<p>Expressions are Javascript, so you can use all the Javascript language elements, including <code>if</code> and <code>else</code>. Thus:</p>

<pre class="lang-js prettyprint-override"><code>maxOffset = 20;
minOffset = 1;
seedRandom(index,timeless=true);
if (time&gt;random(minOffset, maxOffset)){0}else{value}
</code></pre>

<p>This will turn off the layer at some time between 1 and 20 seconds for each layer. If the opacity channel is already animated it will use the animated value up until the time it turns off. To change this so that it is 100% on until it turns off, substitute <code>{100}</code> for <code>{value}</code> in the last line</p>

CC BY-SA 3.0
2015-06-03T05:56:17.730Z
2015-06-12T00:45:22.907Z
2015-06-12T00:45:22.907Z
stib
<p>So, I've a bunch of objects inside a composition and my ideia is the following:</p>

<p>I want to create an expression in one of them that will change the opacity of that object to <code>0</code> after a random time. Then I'm planning to apply this to all objects creating the effect of the objects disappearing at random.</p>

<p>I was looking on the web, and trying to play with this:</p>

<pre><code>maxOffset = 20
seedRandom(index,true);
offset = framesToTime(random(maxOffset));
valueAtTime(time-offset)
</code></pre>

<p>But I can't make it work the way I described. What should happen is:</p>

<ol>
<li>Generate a random frame number between 0 and value of <code>maxOffset</code>;</li>
<li>When the random frame number matches current time change object
opacity to zero;</li>
</ol>

<p>Thank you.</p>

CC BY-SA 3.0
2015-06-02T16:39:40.093Z
Just to get it right, your object disappears completely after a random amount of time and should no longer appear in your comp, right?
p2or
CC BY-SA 3.0
2015-06-02T17:25:56.173Z
@poor exactly! thats what I'm trying to do.
TCB13
<after-effects><after-effects-expressions>
AE: Hide object after random time
TCB13
# AE: Hide object after random time

(Asked by: username_0 on 2015-06-02)

So, I've a bunch of objects inside a composition and my ideia is the following:
I want to create an expression in one of them that will change the opacity of that object to 0 after a random time. Then I'm planning to apply this to all objects creating the effect of the objects disappearing at random.
I was looking on the web, and trying to play with this:
maxOffset = 20
seedRandom(index,true);
offset = framesToTime(random(maxOffset));
valueAtTime(time-offset)

But I can't make it work the way I described. What should happen is:

Generate a random frame number between 0 and value of maxOffset;
When the random frame number matches current time change object
opacity to zero;

Thank you.

**Tags:** after-effects, after-effects-expressions

> **Comments:**
> **username_1**: Just to get it right, your object disappears completely after a random amount of time and should no longer appear in your comp, right?  
> **username_0**: @poor exactly! thats what I'm trying to do.  

---

## 1 answers

### Answer by username_2 on 2015-06-03. Score: 2 | Accepted answer

I'm not sure what the valueAtTime function is trying to achieve. That takes the value of the property at the given time. In the script you wrote it would sample the value of the opacity at a point a random number of seconds in the past, some time between the current time and maxOffset seconds ago. Unless the opacity changes during that time it won't do anything at all.
Expressions are Javascript, so you can use all the Javascript language elements, including if and else. Thus:
maxOffset = 20;
minOffset = 1;
seedRandom(index,timeless=true);
if (time>random(minOffset, maxOffset)){0}else{value}

This will turn off the layer at some time between 1 and 20 seconds for each layer. If the opacity channel is already animated it will use the animated value up until the time it turns off. To change this so that it is 100% on until it turns off, substitute {100} for {value} in the last line


---

