How Do I Find and Access a Text Element of an Entity?

I want my script to find some entity, find its text element and modify the text. As a wild guess I thought it might be something like the following but its not :):

this.modelNameText=this.app.root.findByName("Text_Mattress_Name").element.text;

this.modelNameText= "Hello";

You got it right, though you can’t reference a string value in another property. Text, number and boolean values are always passed by value not by reference in Javascript (unlike objects).

Try doing it like this:

this.modelNameElement = this.app.root.findByName("Text_Mattress_Name").element;
this.modelNameElement.text = "Hello";
2 Likes

Great. Thank you!

1 Like

I tried to do the same for accessing the textures of an image element but get the error:

Cannot create property ‘_glTexture’ on string ‘P’

My code:

 this.mattressImage01=this.app.root.findByName("Mattress_Main_Image").element;
this.mattressImage01.texture=this.appManagerScript.mattressData.brands[this.appManagerScript.currentBrand].products[this.appManagerScript.currentMattress].images[0];

…where my JSON image[0] is “someMattress.png”

Maybe image elements are handled differently or do I need to define the full project path to the texture in my JSON?

element.texture is expecting a pc.Texture object, not a string https://developer.playcanvas.com/en/api/pc.ElementComponent.html#texture

1 Like

If that is the name of your image asset you need to grab it from the assets registry first and then use its texture resource:

var imageName = this.appManagerScript.mattressData.brands[this.appManagerScript.currentBrand].products[this.appManagerScript.currentMattress].images[0];

var imageAsset = this.app.assets.find(imageName);

if( imageAsset && imageAsset.loaded){
   this.mattressImage01.texture= imageAsset.resource;
} 
2 Likes