Insert html/css in my script

I want to insert html and css in my script and im doing this:

t

and then i get error : Cannot read property ‘resource’ of undefined

When you declare script attributes of type ‘asset’ then when you access them in your script they will already be the instance of the asset, so you don’t have to do this.app.assets.get(...). So in your case this.cssAsset.resource would have your CSS data.

So i should keep the line var style = pc.createStyle(this.cssAsset.resource || ‘’); ?

Yeah it should be var style = pc.createStyle(this.cssAsset.resource); . Also you have to learn how to use the debugger in your browser. That way you can breakpoint at the offending line in your script and check what values your variable have at that moment so you can find out what’s wrong.

Here’s some info on debugging in Chrome here https://developers.google.com/web/tools/chrome-devtools/javascript/

1 Like

Thank you very much!

still get error: Cannot read property ‘resource’ of null

still get error: Cannot read property ‘resource’ of null

Are you able to post a URL to your project so the forum community can help you debug?

So I found out what the problem is there - it’s a bit more complicated. Let me explain:

When you create or modify the script attributes of one of your scripts in the Code Editor you then need to go back to the Editor and hit Parse on that script. That will make the Editor read your script again and update the script attributes. However if there is an error while parsing the script attributes will not be updated.

In your case in that musicButton.js script at line 108 you declare a method using the class name PlayButton instead of MusicButton. This causes script parsing to fail so the cssAsset attribute (which was probably named css before) does not actually exist yet. So to fix this, make sure you fix the PlayButton thing to say MusicButton and then hit Parse again on your script. Then assign assets to your CSS and HTML attributes again and it should work (you might get more unrelated errors later so make sure you debug those).

1 Like

You are right.But the button that i create in css does’t appear.I aslo add the html file.

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

Also "this.entity.sound.stop(“music”);"doesnt work.

Sorry for all these questions but im new and im trying to understand.

reneee

.sound.stop(“name”) only works if already paused. (see https://developer.playcanvas.com/en/api/pc.SoundComponent.html)

.sound.pause(“name”) will work.

1 Like