[SOLVED] Script updating texture affects all entities with script

My project takes a list of models located on my local server and loads them into the scene with a nametag hovering above them. I’m trying to implement the text script from https://playcanvas.com/project/362231/overview/tutorial-canvas-text for the nametags and it’s working well except that as my models load in and create their script, all previously loaded nametags update to the new text.

I’m just using the exact text.js script from the example for now until I can wrap my head around what’s happening.
The script to load the models is

function init_geometry(app){
	var _geometry;
	_geometry = new pc.Entity();
	_geometry.name = "geometry";

	for(var i in wayf.list){
		app.assets.loadFromUrl("./Wayf/"+i+".json", "model", function(err, cbt){
			if(cbt != undefined){
				var id = cbt.name.substring(0,cbt.name.length - 5);
				var modelE = new pc.Entity();
				modelE.addComponent('model', {asset: cbt});
				modelE.name = id;
				var modelLabel = new pc.Entity();
				modelLabel.addComponent("model", {type:"plane"});
				modelLabel.script.create("text", {attributes:{text:modelE.name}});

	return _geometry;

Once it’s loaded it looks like

But each should be a different Id number.
As more models load in I can see the nametags change to the newest so I know it’s partially working.

I’m integrating the playcanvas engine into an existing project so I don’t have a project I can link to, sorry.

Probably that’s because all entities end up using the same material. Inside the text script in initialize you could try cloning the material instead like so:

var material = this.entity.model.material.clone();
material.emissiveMap = this.texture;
material.opacityMap = this.texture;
material.blendType = pc.BLEND_NORMAL;
this.entity.model.material = material;

Also you should probably use the new Element system for this stuff. Manual for that starts here https://developer.playcanvas.com/en/user-manual/user-interface/user-interface-basics/

That solved it! Thanks a ton.
I looked at every other part but I glazed over the material assignment for some reason.

I’m not sure if the Element system will be quite what I need for this, I’ve got some other components to add but I’ll check it out.

Thanks a ton.

1 Like