[SOLVED] Cannot read properties of undefined (reading 'on')


I get this error when it was just working fine until I messed up the script, I rewrote it and can’t get rid of the error from popping up. If I take it out the code won’t work and when I press launch it works fine just has the error now. I’m doing this for a game design class at my school so time is of the essence and it is attached to the enemy if anyone thought it wasn’t attached to anything. the code for the script is:

var Trigger = pc.createScript('trigger');

 // initialize code called once per entity
Trigger.prototype.initialize = function() {
this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

 // update code called every frame
Trigger.prototype.update = function(dt) { 
if (this.app.timeScale == 1) {
         this.entity.translate(0, 0, -0.1);
}};
Trigger.prototype.onTriggerEnter = function (result) {
        this.entity.enabled = 0;  
        result.enabled = 0;
};

Please let me know if there is something wrong with my script or what you think could work to fix my game. I’ve been trying to read how to fix it through multiple forums and I tried copying and replacing the script I had before but still the same error. It worked before so I don’t know what the problem is now. I am in the optimization progress of my game if that helps.

Hi @Fabian_S and welcome,

Make sure the entity that the trigger.js script is attached to, has a collision component.

Thank you for the greeting and It definitely does I even have an enemy spawner with the same enemy entity attached and they all work when you press play/launch

Not sure then what’s the problem. It may be better to share your project, if it’s public or if not possible a sample project that reproduces the issue.

That way it will be possible for people to debug this issue in context.

Not sure if this is the way to do it but the url is
https://playcanvas.com/editor/project/1014125

This happens in the level 1 scene

Hi @Fabian_S! Your Timer entity has also the trigger.js script, but no collision component. Apart from that, I don’t think line 21 of your trigger.js script is on the correct place.

Thank you I haven’t done anything and somehow it is fixed. I did fix the timer entity and suddenly the code stopped showing up thank you for your time to go through check my game and help with input on other problems.

1 Like