• 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
0
Question by RNVitter · Apr 22, 2014 at 04:55 PM · 2dhealthbar

2D sidescroller health bar

I am new to coding and Unity

I have the health bar appearing on my screen, but I am not sure how to link the Health script of the health bar to my player script and my player's health script. Simply, I want to make it so when my player gets shot my health bar will lose a heart. The health script of the player's is also linked to the enemy.

My health bar is made with a GUITexture heart

I used this video for the health bar and now I need to figure out how to incorporate the health bar script with my health script and and player script

I get the impression that I need to include some lines of code from my health bar script or link it/reference it some how in my player script

https://www.youtube.com/watch?v=-z-KLXPPGmg

my code is in the link in the same question that I asked on stack overflow...

http://stackoverflow.com/questions/23207298/unity-2d-health-bar

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
0
Best Answer

Answer by getyour411 · Apr 23, 2014 at 01:08 AM

The simple way to get Script A to interact with Script B is:

On Script A

public ScriptBName scriptBName;

in the Inspector, a field will appear called scriptBName (for the gameobject Script A is attached to)

Drag/drop the gameobject that Script B is attached to into that field; voila

Now in Script A you can access (public) fields, methods etc on Script B by doing something like

 if(scriptBName.someVar) //

or

 if(PlayerGotSmacked)
 scriptBName.health --;
Comment
Add comment · Show 1 · 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 RNVitter · Apr 23, 2014 at 01:14 AM 0
Share

It worked! You must be an angel man, I've been stuck on this forever! Thanks a lot!

avatar image
0

Answer by koray1396 · Apr 22, 2014 at 05:20 PM

I can not go over all the scripts there, but as a simple way;

 int = numberOfLives;
 public List<GUITexture> hearts; // or array, does not matter, you should assign GUITexture objects from the inspector, 5 of them if you have a max of 5 lives
 void HealthBar(){
     for(int i = 1; i <= hearts.Count; i++){
        if(i <= numberOfLives){
             hearts[i].gameObject.SetActive(true);
        } else {
             hearts[i].gameObject.SetActive(false);
        }
     }
 }

Comment
Add comment · Show 3 · 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 RNVitter · Apr 22, 2014 at 05:31 PM 0
Share

which script would I put that under?

avatar image koray1396 · Apr 22, 2014 at 06:11 PM 0
Share

does not matter, you can put it into any script, you only need to track number of lives. you would call HealthBar() only when needed, such as when a life is lost etc... by looking at your "Health" script, i'm not sure how you calculate the health as an integer, it's far too complex for such thing.

okay, here's a quick simpler one.

 public List<GUITexture> hearts;
 int numberOfLives; 
 public float startHealth;
 float currentHealth;
 public float heartSpace;
 
 void Awake(){
     PlaceHearts();
     HealthBar();
 }
 
 void HealthBar(){
     for(int i = 1; i <= hearts.Count; i++){
        if(i <= numberOfLives){
             hearts[i].gameObject.SetActive(true);
        } else {
             hearts[i].gameObject.SetActive(false);
        }
     }
 }
 void Update(){
     numberOfLives = $$anonymous$$athf.CeilToInt((currentHealth / startHealth) * hearts.Count);
     HealthBar();
 }
 
 void PlaceHearts(){
     for(int i = 0; i < hearts.Count; i++){
         hearts[i].pixelInset = new Rect(Screen.width * (0.1f + (i * heartSpace)), Screen.height * 0.9f, Screen.width * 0.1f, Screen.width * 0.1f);
     }
 }
avatar image RNVitter · Apr 22, 2014 at 06:17 PM 0
Share

I'm not keeping track of lives but hitpoints, the player has a 100 points of health and a each heart equals 10 points of health. I have all the code for the health bar and it appears on the screen but nothing happens to the health bar when the player takes damage so I get the impression that I need to include some lines of code from my health bar script or link it/reference it some how in my player script

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Scaling related to damage 2 Answers

Health subtracts when hit by bullit 2 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

[2D][Beginner]How do I heal my character when pressing a key while next to a health pack? 1 Answer

2D Animation does not start 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