[SOLVED] Entity attribute is undefined

Newly started on PlayCanvas, got an undefined attribute error (TypeScript : this.attribute is undefined) using the below script. Can anyone help me ?

var Display = pc.createScript('display');
Display.attributes.add('firstScreen', {type: "entity"});
Display.attributes.add('secondScreen', {type: "entity"});

Display.prototype.initialize = function() {
    this.app.on("input.selected", selected);

function selected() {
        console.log("display update");

The input.selected event is fired when a button is clicked, then calling the selected function, it’s breaking with this attribute undefined error on

Of course, I’ve set attributes on editor :

Hi @demdem and welcome! Can you try to replace line 6 with the rule below?

this.app.on("input.selected", selected, this);

Hi @Albertos, thanks for your answer
I did, and it fix the error.

But now it’s the firstScreen.model who is undefined.
Did I make a mistake trying to hide the entity ?

Hi @demdem,

Does your firstScreen entity has a model component?

If not, then it’s expected to be undefined.

1 Like

Hi @Leonidas,

Indeed ! I didn’t even noticed that model is a component.

I don’t have model on my entity, so I’ve replaced the line 11 by :
this.firstScreen.enabled = false;
Is this a correct way to hide and show items ?

1 Like

Yes, that’s correct!

1 Like

Thanks for ur answers ! Helps a lot

Can anyone bring further explanation or a link about @Albertos answer ?

Because documentation says scope is by default the current object, so why not specifying the scope is wrong here ?

Because you are listening to an event on the app object, the current ‘this’ is the app object, not the scriptType you are calling it from

1 Like