# Using a variable for an instance name?

**URL:** <https://forum.kirupa.com/t/using-a-variable-for-an-instance-name/322003>\
**Category:** Uncategorized\
**Created:** [May 6, 2011, 9:24pm UTC](https://forum.kirupa.com/t/using-a-variable-for-an-instance-name/322003 "2011-05-06T21:24:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fademusic](https://avatars.discourse-cdn.com/v4/letter/f/8e7dd6/32.png) [@fademusic](https://forum.kirupa.com/u/fademusic)\
**Post date:** [May 6, 2011, 9:24pm UTC](https://forum.kirupa.com/t/using-a-variable-for-an-instance-name/322003/1 "2011-05-06T21:24:17Z")

</div>

Ok, I’ve been at this for 2 days now, and really need some assistance. It seems like it should be so simple! Tried some other web resources but so far no luck.

The part that doesn’t work:

```
 Code:
 on(release){
//None of these work:
critter_pressed_new.gotoAndStop("poof");
_root[critter_pressed_new].gotoAndStop("poof");
[critter_pressed_new]gotoAndStop("poof");
this[critter_pressed_new].gotoAndStop("poof");

```

}  
Where, “critter\_pressed\_new” is not a specific instance name, but rather a variable name.

Please see the attached file and take a look if you are familiar with variables.

If you want more info, here’s the deal…

I randomly launch some movie clips from the library with attachMovie (they are bedbugs). No problem there.

On launch they get instance names. The first one gets “critter\_launched\_01”, the second “critter\_launched\_02”.

There is a button in the root that needs to be able to make the critter go away (by going to a frame label of “poof”, which has a red X on it for now).

I have a little test button that is hard-coded for:  
Code:  
on(release){  
//this is what code needs to do from other button, but using var name:  
\_level0.critter\_launched\_01.gotoAndStop(“poof”);  
\_level0.critter\_launched\_02.gotoAndStop(“poof”);  
}  
and it works fine. No problem there.

But, I need to be able to have one button close only the most recent bug. So I created a variable in the root to keep track of the last one out:

```
 Code:
 var critter_pressed_new = "test"; 

```

Then within the critter mc I update it using this:  
Code:  
\_root.critter\_pressed\_new = targetPath(this);  
which now makes the variable of “critter\_pressed\_new” have the instance name of the movie clip in it (which, would be \_level0.critter\_launched\_01 or 02).

Then that button in the root should be able to take the value that is in the variable “critter\_pressed\_new” and use that as the instance name, to make that particular mc go away.

Please help if you can offer any advice at all!! Thank you!
