What determines if entiry.script is there or not? I have a “prefab” of my Player - a (disabled) enity with a model, collision and rigifbody + some scripts like playerMovement.js.
The project in question is private and I’d rather not share it publicly. Can you even access private projects?
Also turns out my code in my original post the issue is not present…although in the console when I do console.log(“player1”), the .script property is missing:
I did some more research, and I think its my player initialization code that causes the problem afrer all, I have to dig into it. It works on the first time, but after destroying a player, cloning a new one and then trying to access other script via script.someOtherScriptOnMyPlayerEntity, the .script is undefined.
Yeah, I am setting up a dummy project that would reproduce the issue. I’ll post it once I get there.
However, I think I might have an idea why I am getting this entity.script beign undefined (which then least to Uncaught TypeError: Cannot read property ‘playerMovement’ of undefined) - my game is a multiplayer game where the player entity gets destroyed on disconnect, and (re)cloned on connect. For some reason, the destroy() does not actually destroy the player entity of the first session, and on the second session when I click the mouse button to move the player, the code on the player entity of the first session gets invoked.
Does this sound like it might be the issue to you guys? I tried adding event unscubscription on destory, but that didnt solve the issue. This is essesntially what I have now:
Nevermind the game itself, just click anywhere on the game with a mouse and observer the error “Uncaught TypeError: Cannot read property ‘testScript’ of undefined” in the console.
The error occurs in Movement.js on line 38 because for some reason, the mouse click code is invoked in the already destroyed player instance. The player cloning and destorying happens in manager.js. If you remove the destroying and cloning the second player, there is no issue.
Ah so, this makes it clearer what the issue is, update your on destroy callback to remove the event listener on the mouse instance, not on the application instance:
Mmm, I haven’t encountered ever any issue with calling destroy() that wouldn’t destroy and remove the entity from the entity hierarchy.
The behavior you were getting due to the error, is mostly JS related. A JS object will be cleared and garbage collected when all references to it has been released. Because you weren’t removing the event handler from the deleted entity, its JS object reference was still available in memory that’s why your console.log(entity._guid) worked and printed a guid.
That wasn’t a guid of an entity that existed at that instant in the Playcanvas scene hierarchy though, it was successfully destroyed as far as Playcanvas is concerned.