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 …
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.
try taking out Texture from guiTexture