# \[CS3/AS2\] attachMovie TO an MC using variable?

**URL:** <https://forum.kirupa.com/t/cs3-as2-attachmovie-to-an-mc-using-variable/330090>\
**Category:** flash\
**Created:** [November 28, 2012, 11:33pm UTC](https://forum.kirupa.com/t/cs3-as2-attachmovie-to-an-mc-using-variable/330090 "2012-11-28T23:33:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![allthumbs](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@allthumbs](https://forum.kirupa.com/u/allthumbs)\
**Post date:** [November 28, 2012, 11:33pm UTC](https://forum.kirupa.com/t/cs3-as2-attachmovie-to-an-mc-using-variable/330090/1 "2012-11-28T23:33:12Z")

</div>

In a nutshell, I’ve got many (hundreds of) different movieclips on stage that I have just generated using duplicateMovieClip. (this works fine.)

After generating each of the movie clips, I next need to replace them all individually with one of a handful of existing movieclips in the library.

The ‘targets’ (the placeholder MC’s I am trying to attachMovie onto) all exist within a parent MC. i.e.

```auto

_root.parent_mc 
_root.parent_mc.duplicated_mc_1
_root.parent_mc.duplicated_mc_2
_root.parent_mc.duplicated_mc_3
_root.parent_mc.duplicated_mc_4
_root.parent_mc.duplicated_mc_5

```

etc… etc…

There’s a LOT of these duplicated MC’s, so I’m trying to use attachMovie within a loop to do the changing.

If I hard code the target into the script things work just fine. For example, this works as expected…

```auto
_root.parent_mc.duplicated_mc_1.attachMovie("symbol1", x, 0);

```

…where symbol1 is an MC in the library with the appropriate linkage/exported to first frame stuff for the actionscript.

However, I can’t seem to make things work using a variable in the same string.

In my loop I have created a variable to hold the identifier for the MC to be replaced. Something like:

```auto

for (i=1;i<100;i++){
    var y:String = "duplicated_mc_" + i;
    // Outputs duplicated_mc_1, duplicated_mc_2, etc. etc. up to 99.
}

```

Now I need to address the target MC using that y in a loop, like:

```auto
_root.parent_mc.y.attachMovie("symbol1", x, 0);

```

That doesn’t seem to work however, nor do various other things I’ve tried like

```auto

_root.parent_mc.this[y].attachMovie("symbol1", x, 0);
_root.parent_mc.[y].attachMovie("symbol1", x, 0);

```

etc… etc…

Tracing y in the same place gives me the same string as the hard coded value, so  
I’m assuming I’m getting the syntax wrong, but all the info I can find seems to focus on putting dynamic variables within the brackets - which isn’t what I’m having trouble with (those I can seemingly substitute ok.)

Sorry if this is more obvious that I imagine, any advice much appreciated!
