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 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();

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 (
at Health.reduce (
at Player.updateAnimation (
at Player.update (
at Func._updateInstances (
at Func.onUpdate (
at (
at Function.update (
at Application.update (

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 =;

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