Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Sermer · Oct 15, 2015 at 10:54 AM · prefabenemyhealthbar

Wrong prefab is changing

I have spent several days on this issue and could really use some help!

In my game, I have a working enemy health system. It has been tested extensively and is working flawlessly. To go with the health system, I want to have a health bar above the enemy's head. The health bar also works flawlessly so long as there is only one enemy.

The problem is that I have multiple prefabs that get spawned in that all need to behave individually. Each prefab's health behave's correctly and the health bar follows them correctly. What doesn't work correctly is that whenever a prefab takes damage, it only changes the health bar of the first prefab that spawned. If that prefab is destroyed, it just switches the the oldest existing prefab.

I'll post the health bar code below:

using UnityEngine; using System.Collections;

public class HealthBarChanger : MonoBehaviour {

 private static Vector3 size;
 private static bool hasChanged = true;

 public Sprite Health0;
 public Sprite Health1;
 public Sprite Health2;
 public Sprite Health3;
 public Sprite Health4;
 public Sprite Health5;
 public Sprite Health6;
 public Sprite Health7;
 public Sprite Health8;
 public Sprite Health9;
 public Sprite Health10;

 private static float Health = 1, TotalHealth = 1;

 private void FixedUpdate(){
     if(hasChanged){
         if((Health/TotalHealth) == 1){
             GetComponent<SpriteRenderer>().color = new Vector4(0,0,0,0);
             GetComponent<SpriteRenderer>().sprite = Health9;
         }else{
             GetComponent<SpriteRenderer>().color = new Vector4(1,1,1,1);
         }

         if(Health >= 0){
             transform.localScale = size;
             Debug.Log(size);
         }else{
             size.x = 0;
             transform.localScale = size;
         }
         if((Health/TotalHealth) < .1){
             GetComponent<SpriteRenderer>().sprite = Health0;
         }
         else if((Health/TotalHealth) < .2){
             GetComponent<SpriteRenderer>().sprite = Health1;
         }
         else if((Health/TotalHealth) < .3){
             GetComponent<SpriteRenderer>().sprite = Health2;
         }
         else if((Health/TotalHealth) < .4){
             GetComponent<SpriteRenderer>().sprite = Health3;
         }
         else if((Health/TotalHealth) < .5){
             GetComponent<SpriteRenderer>().sprite = Health4;
         }
         else if((Health/TotalHealth) < .6){
             GetComponent<SpriteRenderer>().sprite = Health5;
         }
         else if((Health/TotalHealth) < .7){
             GetComponent<SpriteRenderer>().sprite = Health6;
         }
         else if((Health/TotalHealth) < .8){
             GetComponent<SpriteRenderer>().sprite = Health7;
         }
         else if((Health/TotalHealth) < .9){
             GetComponent<SpriteRenderer>().sprite = Health8;
         }
         else{
             GetComponent<SpriteRenderer>().sprite = Health9;
         }
         hasChanged = false;
     }
 }

 public void ChangeBar(float health, float totalHealth){
     Health = health;
     TotalHealth = totalHealth;
     size = transform.localScale;
     size.x = health;
     size.y = 8;
     hasChanged = true;
 }

}

And this is the code that invokes ChangeBar:

 public bool DealDamage(float damageDealt){
     health -= damageDealt;

     healthBar.GetComponent<HealthBarChanger>().ChangeBar(health, 100f);
     damageDealt = 0;
     return (health <= 0);
 }

Like I said, everything works except that it changes the wrong healthbar. Any help would be greatly appreciated!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Sermer · Oct 15, 2015 at 08:45 PM

Well the problem still baffles me but I was able to make a workaround. I moved all of the healthbar code into DealDamage except for the code that makes the bar invisible/visible which I put into a LateUpdate. For some reason it was just passing the wrong GameObject in between scripts but it uses the right one within the single script.

It's ugly and I wish I had a more elegant solution, but it works.

P.S. I'll give Best Answer if anyone can still solve my original problem!

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

32 People are following this question.

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

Related Questions

Prefabs executing in a wrong way. 0 Answers

Instantiated prefab with ui child gameobject , works differently compared to on-scene prefab 0 Answers

Attacking an enemy (2D platformer) 0 Answers

Im using raycast system for shooting but cant figure out how to add blood partical to an enemy collider 0 Answers

Help with a health bar 0 Answers


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