[SOLVED] Unknown problem with collider script

Hello I have a few problems with some scripts that I don’t know why:About 2 problems.

I will go over this first one though.

So I keep getting this error in my script.

I go to the script and everything looks fine.

> 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.

Hi @DinoCalebProductions! Has the entity with the script a sound component?

yes it does it has a sound and a colider with a rigidbody

I could give you a link to the game if you would like.

https://playcanvas.com/editor/scene/1480456

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.

Ok thankyou @Albertos I guess I did not see that. yes I will fix the other errors too.

thankyou very much.

I see the script itself has the name colider.js instead of collider.js.

I changed the name from colider.js to collider.js, but now it gives me this when I parse.

Screenshot 2022-09-23 2.36.06 PM

Should I just delete this one and make a new script to replace it?

You use that script on a lot of entities, so I don’t suggest to remove it. Then maybe keep it as it was.

so i fixed this problem
Screenshot 2022-09-23 2.36.06 PM

it doesnt have that error anymore, and i didnt have to delete the script. but it still says cannot read properties of undefined (reading play).

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.

ok thanks for your help @Albertos

your welcome

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.

Thanks for all the help though.

Have a nice day!

Maybe you can try use the statement below to avoid the error.

if (result.other.collision && this.entity.sound) {
    // play sound
}

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.

1 Like

Hello.

I just used the one @Albertos gave me and it worked perfectly plus the error is gone.

so thank you very much for your help.

I may also use the script @yaustar gave me too.

thank you very much and have a nice day!