- Home /
Unity messing TextMesh related variables?
I have a enemy spawner that spawns an enemy every 5 seconds.
Enemies have a 10-hit health bar, made from a TextMesh and converting an array to string, and then attaching it to the "Text" variable of TextMesh.
It works fine with one enemy on scene, but, if another enemy spawns (that's why i increased the health bar) something extrange occurs. I will explain it by points:
Enemy(1) spawns. 10HP
Enemy(1) moves towards destination (using unity's pathfinding) while recieving hits from tower.
Enemy(1) has 3HP, spawner interval reaches 0;
Enemy(2) spawns with 10HP, but no health bar. Enemy(1) gets Enemy(2) health bar, but it does not update (remains on 10 HP);
Enemy(1) is destroyed since the health variables are ok (went from 3 to 0).
Enemy(2) gets back its health bar and everything works fine until...
Enemy(3) spawns and all process start again.
Since i'm working on JavaScript, this is the health bar making:
function Update(){
var tm : TextMesh = GameObject.Find("Health").GetComponent(TextMesh);
var healthArray = new Array();
for(i=0;i<currentHealth;i++){
healthArray[i]="-";
}
healthString= Array(healthArray).Join("");
tm.text = healthString;
tm.renderer.material.color = Color.red;
tm.transform.forward = Camera.main.transform.forward;
.....
I think that the problem is the TextMesh reference, which is getting the wrong one to operate, but i'm not sure at all.
GameObject.Find()
simply returns the first matching object in the scene. Do you have a unique Text$$anonymous$$esh attached to each enemy? If so, you should probably find that particular enemy's Text$$anonymous$$esh ins$$anonymous$$d of searching the entire scene.
Actually the Text$$anonymous$$esh which is called "Health" is part of the enemy prefab. Enemy prefab is composed by a model taken from Angry Bots (parent object) and then the "Health" Text$$anonymous$$esh(children of the model).
I've tried GetComponentInChildren, but i does not work (it does the same). Now you said that about .Find(), i think that might be the problem. Since i call it every update, it will take the first enemy spawned.
How i can manage that?
Your answer
Follow this Question
Related Questions
Enemy Spawning Place 0 Answers
Enemy not spawning correctly 1 Answer
More Efficient Enemy Spawning Than This? 2 Answers