Why does my script not work?

var DoorOpener = pc.createScript('DoorOpener');

DoorOpener.attributes.add('Door', {type: 'entity'});

if(this.gameStarted === true)
    
  if (app.keyboard.isPressed(pc.KEY_E)) {
  this.Door.enabled = false;
 

   if (app.keyboard.isPressed(pc.KEY_Q)) 
   this.Door.enabled = true;

I have no clue why it is not working.

Hey, you will have to write the game logic within some function to make it work. You can write it in the function like this:

DoorOpener.prototype.update = function (dt) {
this.gameStarted = true;
 if(this.gameStarted === true) {

if (app.keyboard.isPressed(pc.KEY_E)) {
this.Door.enabled = false;

if (app.keyboard.isPressed(pc.KEY_Q))
this.Door.enabled = true;
}
};
1 Like

Thanks for your response, but your code is giving this error: Uncaught SyntaxError: Unexpected end of input

SyntaxError: Unexpected end of input

Seems like a bracket issue. Can you share your complete script code here?

1 Like

sure.

var DoorOpener = pc.createScript('DoorOpener');

DoorOpener.attributes.add('Door', {type: 'entity'});

DoorOpener.prototype.update = function (dt) {
this.gameStarted = true;
 if(this.gameStarted === true) {

if (app.keyboard.isPressed(pc.KEY_E)) {
this.Door.enabled = false;

if (app.keyboard.isPressed(pc.KEY_Q))
this.Door.enabled = true;
}
};

Can’t see any issue here except with adding “this” before the keyboard condition like this:

var DoorOpener = pc.createScript('DoorOpener');

DoorOpener.attributes.add('Door', {type: 'entity'});

DoorOpener.prototype.update = function (dt) {
this.gameStarted = true;
var app = this.app;
if(this.gameStarted === true) {

if (app.keyboard.isPressed(pc.KEY_E)) {
this.Door.enabled = false;
}
if (app.keyboard.isPressed(pc.KEY_Q))
this.Door.enabled = true;
}

};

If that doesn’t resolve the issue then you can share the link of the project to further look at

Is the script complete, because if it is I’ll test it.

Yes it is.

Okay, give me a moment.

https://launch.playcanvas.com/1327780?debug=true Here is the launch the black part on the side of r=the wall is the door, the code is supposed tomake it disable so, it is then able to let you leave, however it is not working.

Test it.

I have a solution, I can do away with the script, and make the doors non-collision.

The end bracket of the first statement is missing.

Yes, i have resolved that here https://playcanvas.com/editor/scene/1328907 @Jacob_Mcbride

1 Like