Problem creating entity in code

Hello all,

I’m new in PlayCanvas and didn’t use Javascript before.
I got problem when tried to create entities using code. I followed several tutorials and created script that should receive notifications and create entities with ‘plane’ model type.
I got this error “Uncaught TypeError: Cannot set property ‘scene’ of undefined” when trying to add new entity to root. Somehow, it says that my model is null. I spent 2 days trying to figure out what’s wrong with it. I searched for similar problem on forum, used debugger and console, but without success :frowning:

I don’t know what’s wrong with it. Please, help me!
It would be great if someone could check test project here: https://playcanvas.com/editor/scene/535629
Problems are in Test.js file, trigger is a button with ‘Boost’ title (BoostButton.js).

Thanks in advance!

Hi.

Your problem is that you mix up assets and materials

When you import asset, you should use it’s resource, but when you create material, it’s ready to be attached to model.

So, instead of:

    var mat = new pc.StandardMaterial();
    mat.emmissiveMap = asset;
    mat.opacityMap = asset;
    mat.opacityMapChannel = 'a';
    
    var entity = new pc.Entity();
    entity.addComponent('model', {type:'plane'});
    entity.model.material = mat.resource;

You have to:

    var mat = new pc.StandardMaterial();
    mat.emmissiveMap = asset;
    mat.opacityMap = asset;
    mat.opacityMapChannel = 'a';
    
    var entity = new pc.Entity();
    entity.addComponent('model', {type:'plane'});
    entity.model.material = mat;

Thanks, that helped!