Problem with assets.find?

Hello!

I was trying to use assets.find to populate an entity on the fly but it keeps returning null.
I tried the model name, the filename and the ID but same result.
I made a test project here:
https://playcanvas.com/editor/scene/1139987

Hi @iXperiential,

The biggest thing goign on with your script was that when searching for an asset by name you need to include the full name as it appears in the editor. In this case the asset in the editor’s name includes the file extension.

You will find that doing this:

TestFind.prototype.initialize = function() {
    
    this.jenny = this.app.assets.find("SM_Jenny.glb", "model");
    if(this.jenny !== null)
        console.log("Success !");
};

should work for you.

2 Likes

It needs to be the full filename. You example didn’t include the .glb: https://playcanvas.com/editor/scene/1139991

With the ID, it worked for me. Please note that it needs to be used with get function: https://developer.playcanvas.com/en/api/pc.AssetRegistry.html#get

1 Like

Weird. I could have sworn I tried the full filename. Thanks!

Since we’re here, I tried to add some code to add a model on the fly.
I see the model in the hierarchy when I debug but it doesn’t show up. Is there anything I’m missing?
I modified the same project:

this.jenny = this.app.assets.find("SM_Jenny.glb", "model");
var self = this;
if(this.jenny !=null)
{
    var head = new pc.Entity();
    head.name = "MyHead";
    head.addComponent("model", {
        type: 'asset',
        asset: self.jenny
    });
    head.setLocalScale(1,1,1);
    head.setPosition(0,2,0);
}

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

Your forgetting to add it as a child to the root.
insert this into the if statement self.addChild(head)

@iXperiential

Thanks! I know I was missing something silly.

1 Like