[SOLVED] Error for enabling entities

Hello,
I have an entity I am trying to turn on,

Inventory.attributes.add('sscreen', {
    type: "entity",
});

but when I do this.sscreen.enabled = true I get this error: Cannot read properties of null (reading ‘enabled’) I have set the entity in the script attribute. What am I doing wrong?

The error means the attribute is empty. Most probably the entity that it is referencing is not available yet (or no longer) when you call that code. Is it in the scene in the Editor? Are you changing scenes?

I am not changing scenes, it is a scene in the editor.



Is this the line where the error happens? It should work. Can you screenshot the error from the console?

Click the link on the right side of the console to jump to the place in code.

It seems to be directing me to this:

Ah, I meant to the right of the line, sorry, should jump to the actual code in update method:
image

I suspect this one is culprit, since it is empty:
image

I set that to a placeholder entity and I am still getting the error.

And it is the same piece of code that is getting the error

if(this.sscreen.enabled === true){
console.log('sscreen is on'); 
};

From the info you provided, it should work fine. Must be something else. If you have a project link, then someone could take a look.

Ok. My project is a little messy. PlayCanvas 3D HTML5 Game Engine The scene is Class Select. If you click on Alpha and then Character Controler, that entity has the inventory script

Probably you added an Entity to the attribute, but didn’t apply the change to the template?

What does that mean?

When you change anything in a template, you won’t see the changes in Launcher, until you apply them:

image

It works. But I am still getting the error. Strange.

I disabled the original code that was giving the error as it was a test. Now this this.sscreen.enabled = true; is giving the error

You have:

this.screen.enabled = false;
this.sscreen.enabled = false;

The first one is a Screen Component, which doesn’t exist on this entity.
The second one is your attribute.

Ok. I didn’t mean to have a screen component. I had a script element named screen I’ll change it and see what happens.

The other scripts on entity should be accessed via Script Component (this.script), e.g.:

this.script.myScript

Ok. I think I have everything figured out, I just need to smooth out some code. Thanks for all your help!

1 Like