Problem with material when clone entity

Hello, in my code I clone the enemy entity but I have a problem with the material used for the health bar
https://playcanvas.com/editor/code/352037/health.js this is the script of health bar, the material used as attribute must be unique for each health bar so it gives me an error while I clone the same entity multiple times, how can I solve this??
Edit: maybe something like this?

    var Material = app.root.findByName('enemy').script.remainingMaterial;
    var newMaterial[i] = Material.clone();
    newEnemy.script.remainingMaterial=newMaterial[i];

Let me know please

What is the error that you are getting?

[health.js:72]: Uncaught TypeError: Cannot read property ‘setLocalScale’ of undefinedTypeError: Cannot read property ‘setLocalScale’ of undefined
at Health.setHealth (http://playcanvas.com/api/files/code/352037/directory/health.js:72:27)
at Health.reduce (http://playcanvas.com/api/files/code/352037/directory/health.js:77:18)
at Player.updateAnimation (http://playcanvas.com/api/files/code/352037/directory/player.js:617:39)
at Player.update (http://playcanvas.com/api/files/code/352037/directory/player.js:398:18)
at Func._updateInstances (https://code.playcanvas.com/playcanvas-stable.js:21007:22)
at Func.onUpdate (https://code.playcanvas.com/playcanvas-stable.js:21011:10)
at Function.fire (https://code.playcanvas.com/playcanvas-stable.js:571:37)
at Function.update (https://code.playcanvas.com/playcanvas-stable.js:18696:23)
at Application.update (https://code.playcanvas.com/playcanvas-stable.js:18291:24)
at https://code.playcanvas.com/playcanvas-stable.js:18602:11

That doesn’t look like it is related to materials as the error is about not being able to find ‘setLocalScale’ on an object that is undefined.

this.remaining is the problem…so i guessed is the remainingMaterial but not sure

Have you used the debugger at all to breakpoint at the error and try to see what’s going on?

Yes just did, when the enemy is activated there is no errors, when the player hit it and the health must decrease it give the error, have to check the damage code i guess

Without seeing the project, I’m guessing that you are cloning the entity and calling setHealth on health.js before health.js initialization has been called as ‘this.remaining’ is undefined.

This is because initialization doesn’t get called until you add it to the hierarchy (this isn’t clear in the documentation which I will fix soon).

So you want something similar to this:

var newBox = this.box.clone();
this.entity.parent.addChild(newBox);
newBox.script.health.setHealth(100);

No @steven i guess the problem is that i referred to the entity as enemy like when i had just one…now i have more so i have to get the enemy id and refer to that or remaning will be undefined

Sorry but i’m struck i have variable ID inside enemy script different for each enemy, when i click on enemy i need to find the enemy with the right ID to: get position and assign damages… i’m lost in code. I get the id when i click on enemy, and after? How do i pick the enemy with that id, get that entity and use the values inside the enemy script?

There are various ways you can do this:

  • Have an enemy manager where you can query search by ID to get the entity
  • Tag all the enemies so you can search by tag and then search through that subset to get the ID
  • When you click on the enemy, I’m assuming you will have the reference to the entity so you can pass that reference to the relevant code

This is more or less the same problem you had with the level loading.

Maybe i have a mind block on that kind of problems lol