# FindByName Enable / Disable object

**URL:** <https://forum.playcanvas.com/t/findbyname-enable-disable-object/20091>\
**Category:** Help & Support\
**Created:** [May 11, 2021, 11:30am UTC](https://forum.playcanvas.com/t/findbyname-enable-disable-object/20091 "2021-05-11T11:30:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maarten\_deVries](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/maarten_devries/32/14885_2.png) [@Maarten\_deVries](https://forum.playcanvas.com/u/Maarten_deVries)\
**Post date:** [May 11, 2021, 11:30am UTC](https://forum.playcanvas.com/t/findbyname-enable-disable-object/20091/1 "2021-05-11T11:30:58Z")

</div>

I don’t know what I am doing wrong in this code. I have assigned this code to an Entity with a collider on it.

Now when I enter the volume I get this error:  
“Uncaught TypeError: Cannot set property ‘enabled’ of undefined”

I also placed a console.log on it to see if the object in the scene is found. And it is found.

```javascript
var TriggerVolume = pc.createScript('triggerVolume');

// initialize code called once per entity
TriggerVolume.prototype.initialize = function() 
{
    this.TriggerZone = this.app.root.findByName("Zone2");
    
    
    this.entity.collision.on('triggerenter', function (entity) 
        { 
        this.TriggerZone.enabled = false;
        console.log(this.TriggerZone + ' has entered trigger volume.');
        }
    );
    
    this.entity.collision.on('triggerleave', function (entity) 
        {
        console.log(entity.name + ' has left trigger volume.');
        }  
    );
};

```

Hope someone can help me out.

Best,  
Maarten

---

<div class="post-metadata">

**Author:** ![eproasim](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/eproasim/32/22818_2.png) [@eproasim](https://forum.playcanvas.com/u/eproasim)\
**Post date:** [May 11, 2021, 11:36am UTC](https://forum.playcanvas.com/t/findbyname-enable-disable-object/20091/2 "2021-05-11T11:36:50Z")

</div>

Hi @Maarten_deVries,

It looks like you’re having problems with scope, so when you are referring to `this` in your collision event, it is referring to the scope within the collision function. To solve for this, you have two options. The first is to use a `self` variable that you can reference back to like this:

```javascript
var TriggerVolume = pc.createScript('triggerVolume');

// initialize code called once per entity
TriggerVolume.prototype.initialize = function() 
{
    var self = this;
    this.TriggerZone = this.app.root.findByName("Zone2");
    
    
    this.entity.collision.on('triggerenter', function (entity) 
        { 
        self.TriggerZone.enabled = false;
        console.log(self.TriggerZone + ' has entered trigger volume.');
        }
    );
    
    this.entity.collision.on('triggerleave', function (entity) 
        {
        console.log(entity.name + ' has left trigger volume.');
        }  
    );
};

```

The other option is to pass along the context to your collision function like this:

```javascript
var TriggerVolume = pc.createScript('triggerVolume');

// initialize code called once per entity
TriggerVolume.prototype.initialize = function() 
{
    this.TriggerZone = this.app.root.findByName("Zone2");
    
    
    this.entity.collision.on('triggerenter', function (entity) 
        { 
        this.TriggerZone.enabled = false;
        console.log(this.TriggerZone + ' has entered trigger volume.');
        }, this);
    
    this.entity.collision.on('triggerleave', function (entity) 
        {
        console.log(entity.name + ' has left trigger volume.');
        }, this);
};

```

---

<div class="post-metadata">

**Author:** ![Maarten\_deVries](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/maarten_devries/32/14885_2.png) [@Maarten\_deVries](https://forum.playcanvas.com/u/Maarten_deVries)\
**Post date:** [May 11, 2021, 11:41am UTC](https://forum.playcanvas.com/t/findbyname-enable-disable-object/20091/3 "2021-05-11T11:41:30Z")

</div>

thank you for the quick reply and clear explanation. I got my script working before I read this reply.  
I however did it different then your answer.

```javascript
var TriggerVolume = pc.createScript('triggerVolume');

// initialize code called once per entity
TriggerVolume.prototype.initialize = function() 
{
    var TriggerZone = this.app.root.findByName("Zone2");
    
    
    this.entity.collision.on('triggerenter', function (entity) 
        { 
            TriggerZone.enabled = false;
            console.log(this.TriggerZone + ' has entered trigger volume.');
        }
    );
    
    this.entity.collision.on('triggerleave', function (entity) 
        {
        console.log(entity.name + ' has left trigger volume.');
        }  
    );
};

```

Instead of ` this.TriggerZone = this.app.root.findByName("Zone2");`  
I did this: ` var TriggerZone = this.app.root.findByName("Zone2");`

and changed ` this.TriggerZone.enabled = false;`  
to ` TriggerZone.enabled = false;`

Is this also a correct way?

---

<div class="post-metadata">

**Author:** ![eproasim](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/eproasim/32/22818_2.png) [@eproasim](https://forum.playcanvas.com/u/eproasim)\
**Post date:** [May 11, 2021, 11:58am UTC](https://forum.playcanvas.com/t/findbyname-enable-disable-object/20091/4 "2021-05-11T11:58:20Z")

</div>

HI @Maarten_deVries,

That is also a valid solution. The only thing to think about is that using `var TriggerZone = this.app.root.findByName("Zone2");` will only be available inside of your initialize function. If you use `this.TriggerZone = this.app.root.findByName("Zone2");`, It will be available to other top level functions of your scripts, but it is up to you whether you need it.

---

<div class="post-metadata">

**Author:** ![WangNianyi2001](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/wangnianyi2001/32/15522_2.png) [@WangNianyi2001](https://forum.playcanvas.com/u/WangNianyi2001)\
**Post date:** [May 11, 2021, 12:13pm UTC](https://forum.playcanvas.com/t/findbyname-enable-disable-object/20091/5 "2021-05-11T12:13:54Z")

</div>

Yet another typical JavaScript `this` pitfall.

To be brief, `this` reference changes in every `function` scope.  
It will only refer to whatever object you’re calling the function on.

You could use the _arrow function_ introduced in the ES6 standard to avoid this problem.  
Alternatively, you could explicitly specify the `this` reference at runtime by calling the `.call` method of the function’s prototype.
