- Home /
Enemy HealthBar Problems
hello, I'm having trouble with my enemy health bar .. the error is
IndexOutOfRangeException: Array is out of range.
ok .. I speak as I did .. I created a prefab .. using the GUI TEXTURE threw him the script
"Var HealthBarTexture: Texture2D [];
function Update () {
guiTexture.texture HealthBarTexture = [GameController.bar];
} @ script ExecuteInEditMode the error is this line "guiTexture.texture HealthBarTexture = [GameController.bar];"
when the player shoots the object begins to decrease the life, I used for this collision the game controller
static var bar: int = 24;
function Awake () { bar = 24; } and ENEMY COLLISION
"OnTriggerEnter function (col: Collider) { if (col.gameObject.tag == "Laser") { GameController.bar -= 1; health -= 1; Destroy (col.gameObject); "
Please help me I did not know what's wrong .. and when I use the enemy respawn healthbar disappears once and for all ..
Answer by aldonaletto · Jan 16, 2012 at 11:07 PM
Even assuming that "Var is a typo, the syntax has several errors. The first code should be:
var HealthBarTexture: Texture2D[]; // assign the textures in the Inspector
function Update () { guiTexture.texture = HealthBarTexture[GameController.bar]; }
// notice the parenthesis after ExecuteInEditMode: @script ExecuteInEditMode() And the last code:
function OnTriggerEnter(col: Collider) { if (col.tag == "Laser") { // tag can be directly referenced by col GameController.bar -= 1; health -= 1; Destroy(col.gameObject); } }
EDITED: You must make some changes in your scripts:
1- Declare the variable bar in the HealthBarEnemy script:
var HealthBarTexture: Texture2D[]; // assign the textures in the Inspector var bar: int; // declare bar as member variable
function Update () { guiTexture.texture = HealthBarTexture[bar]; }
2- Change the enemy script to access the bar variable in the health bar script. If you're not doing this yet, add to it the creation of the health bar - this will link each enemy to its own health bar at Start:
var hBarPrefab: Transform; // <- drag the health bar prefab here private var hBar: Transform;
function Start(){ hBar = Instantiate(hBarPrefab); }
function OnTriggerEnter(col: Collider) { if (col.tag == "Laser") { // tag can be directly referenced by col health -= 1; Destroy(col.gameObject); // transfer the health to the health bar script: hBar.GetComponent(HealthBarEnemy).bar = health; } } NOTE: I'm assuming the health bar script is called HealthBarEnemy - change the name in GetComponent to the actual script name, if it's different.
thanks for the feedback, aldonaletto!
$$anonymous$$y enemy health bar is in trouble, when I create a clone of my enemy .. I shoot on a single target healthbar and it affects all the bars at the same time .. you can do for my bar of another clone not be affected? He's affecting all the bars until the clones when I shoot an enemy in
The problem is the static keyword: a static variable is unique, thus the variable bar will be the same for all enemies.
How is your health bar? There's a health bar "floating" over each enemy? Or is it single fixed health bar that shows only the health of the enemy you're currently fighting?
the enemy is floating bar, I put 25 hp for him and I put in 25 textures and GUI TEXTURE subtract 1 for each shot .. when I create a clone of the same enemy and shoot at 1, the hp bar drops to all ... looks the picture http://img163.imageshack.us/img163/3530/hpfoto.jpg
I edited my answer to show how to access the health bar without static variables. I don't know how are you presently creating the health bar and adjusting its position to follow the enemy. In principle, I added the health bar creation to the collision script, what eases accessing the health control.
omg !! thanks very much !! voce tb é brasileiro ? eu vi no seu perfil ! muito obrigado Aldo de verdade...
Your answer
Follow this Question
Related Questions
How to make an enemy respawn? 0 Answers
Respawn with enemy contact 1 Answer
Enemy Healthbar 1 Answer
Need help with enemy respawn script 1 Answer
instantiated objects animation making the object reset to 0,0,0?? 1 Answer