Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by mdinizzz · Jan 16, 2012 at 10:41 PM · enemyrespawnhealthbar

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 ..

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mdinizzz · Jan 17, 2012 at 04:34 AM 0
Share

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

avatar image aldonaletto · Jan 17, 2012 at 08:05 PM 0
Share

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?

avatar image mdinizzz · Jan 18, 2012 at 05:51 PM 0
Share

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

avatar image aldonaletto · Jan 19, 2012 at 12:49 AM 0
Share

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.

avatar image mdinizzz · Jan 20, 2012 at 04:51 AM 0
Share

omg !! thanks very much !! voce tb é brasileiro ? eu vi no seu perfil ! muito obrigado Aldo de verdade...

Show more comments
avatar image
0

Answer by supergamespawn · Jan 16, 2012 at 11:08 PM

try taking out Texture from guiTexture

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges