> var Collider = pc.createScript('collider');
>
> // initialize code called once per entity
> Collider.prototype.initialize = function () {
> this.entity.collision.on('collisionstart', this.onCollisionStart, this);
> };
>
> Collider.prototype.onCollisionStart = function (result) {
> if (result.other.collision) {
> this.entity.sound.play("hit");
> }
> };
So I parsed the script, I restarted my computer, I even looked over closely at my code like a thousand times to see if I could find an error,typo, anything that is wrong, and I can’t. The script is perfectly fine and should work.
But the error keeps on popping up again and the script wont work. So I came here for help because I don’t know what to do.
And thank you very much for helping.
I will mention my next error in a different topic.
Not sure what’s exactly the problem but the error shows colider.js while the script is collider.js. I guess this is a parse problem, but I’m not sure. Apart from that, I suggest to fix the other errors first, because this is the third error in the row.
I’m on mobile unfortunately and with a quick look I couldn’t find the cause. I think there is an entity in your project without the sound component, but I’m not sure. Maybe you can disable some entities to see if it solves the problem.
I finished checking all the items, entities, etc in the hierarchy and I found everything in shipshape.
I couldn’t find a single object that had the collider script without the sound component. I did find some objects that had a script component without any scripts so I removed the components from the object but other than that everything should be perfect.
I might double check again but until that, I have no idea what is causing the problem. I could only guess that it might be the engine itself reading the script as an error even though it’s fine but I can’t say for sure.
The issue is that the entity is destroyed in the open script on some of your entities like ‘floor run not safe trigger’ before the callback in collider script is called.
The way to get around this is to destroy the entity on the next postUpdate or end of frame or add the check that @Albertos has mentioned.
Unity gets around this by doing something similar where when destroy is called on the gameObject, it actually gets removed at the end of the frame.
var Open = pc.createScript('Open');
// initialize code called once per entity
Open.prototype.initialize = function () {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
Open.prototype.postUpdate = function (dt) {
if (this._destroy) {
this.entity.destroy();
}
};
Open.prototype.onCollisionStart = function (result) {
if (result.other.rigidbody) {
this._destroy = true;
}
};
The sound still won’t play of course because the entity that is playing the sound is destroyed but you won’t get that error at least in this scenario.