# How to create instances at depth?

**URL:** <https://forum.kirupa.com/t/how-to-create-instances-at-depth/258998>\
**Category:** Uncategorized\
**Created:** [April 30, 2008, 6:31am UTC](https://forum.kirupa.com/t/how-to-create-instances-at-depth/258998 "2008-04-30T06:31:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluemagica](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bluemagica](https://forum.kirupa.com/u/bluemagica)\
**Post date:** [April 30, 2008, 6:31am UTC](https://forum.kirupa.com/t/how-to-create-instances-at-depth/258998/1 "2008-04-30T06:31:54Z")

</div>

What i am trying to do is create instances at random depths, but without any of them getting created at a depth preoccupied by another mc, so i tried to do the following code(dont laugh at it), but the instances are all getting created at same depth…any idea what i should do to fix this?

```auto

while(not this.getInstanceAtDepth(player_depth)==undefined)
 {
    player_depth=Math.ceil(Math.random()*29);
 }       
pid=this.attachMovie("player_mc","pid_"+player_count,player_depth)

```
