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];
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?