I keep getting an error for a code that does work

I keep getting an error for this code

var GunCollision = pc.createScript('GunCollision');
GunCollision.attributes.add('gun', {type: 'entity'});
gun = this.gun;
this.gun.enabled = false;
GunCollision.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    this.entity.collision.on('triggerleave', this.onTriggerLeave, this);
};
GunCollision.prototype.onTriggerEnter = function() {
    this.gun.enabled = true;
};
GunCollision.prototype.onTriggerLeave = function() {
    this.gun.enabled = false;
};

but this works perfectly, because it is a copied script from this

var Collision = pc.createScript('collision');
Collision.attributes.add('screen', {type: 'entity'});
screen = this.screen;
this.screen.enabled = false;
Collision.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    this.entity.collision.on('triggerleave', this.onTriggerLeave, this);
};
Collision.prototype.onTriggerEnter = function() {
    this.screen.enabled = true;
};
Collision.prototype.onTriggerLeave = function() {
    this.screen.enabled = false;
};

this is the error (for gun collision) [uncaught type error: cannot set properties of undefined (setting ‘enabled’)]

Hi @Kyle_3_1415! Are you sure you attached an entity to your attribute? Line 3 and 4 need to be in the initialize function.

I found the problem! line 4 had an enable function, and it didn’t like it (I guess :thinking:). Thanks for the help!

1 Like