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 Dumoria · May 14, 2016 at 01:03 PM · c#gamehealthbarlinkhealth

How to create a link between HealthScript and HealtBar script ?

Hi everyone!

The thing I would like to do is that when my health points' player go down, its health bar goes down too. But I don't know how can I do. Maybe something like create a "link" between these two scripts...? I'm novice in scripting...

These are my HealthScript and my HealthBar script:

using UnityEngine;

public class HealthScript : MonoBehaviour {

 public int hp = 5;


 public bool isEnemy = true;

 void OnTriggerEnter2D(Collider2D collider)
 {
     
     ShotScript shot = collider.gameObject.GetComponent<ShotScript>();
     if (shot != null)
     {
         
         if (shot.isEnemyShot != isEnemy)
         {
             hp -= shot.damage;

           
             Destroy(shot.gameObject);

             if (hp <= 0)
             {
                 
                 Destroy(gameObject);
             }
         }
     }
 }

}

using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI;

public class HealthBar : MonoBehaviour {

 #region Attributes

 public GameObject healthBar;
 public Color goodColor;
 public Color middleColor;
 public Color badColor;

 #endregion

 #region Unity methods

 void Start()
 {
     SetColor();
 }

 #endregion

 #region Other methods

 public void SetDamages(float value)
 {
     healthBar.GetComponent<Scrollbar>().size -= value;

     float totalValue = healthBar.GetComponent<Scrollbar>().size;

     SetColor(totalValue);
 }


 void SetColor(float value = 1)
 {
     if(value >= 0.5f)
     {
         healthBar.transform.FindChild("Mask").FindChild("Sprite").GetComponent<Image>().color = goodColor;
     }
     else if(value >= 0.25f && value < 0.5f)
     {
         healthBar.transform.FindChild("Mask").FindChild("Sprite").GetComponent<Image>().color = middleColor;
     }
     else
     {
         healthBar.transform.FindChild("Mask").FindChild("Sprite").GetComponent<Image>().color = badColor;
     }
 }

 #endregion

}

Could someone help me please? :)

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

Answer by JedBeryll · May 14, 2016 at 01:34 PM

Create a variable for the health bar in the HealthScript like this:

 public HealthBar healthBar;

Then assign it in the inspector.

          if (hp <= 0)
          {
              Destroy(healthBar.gameObject);
              Destroy(gameObject);
          }
Comment
Add comment · Show 5 · 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 Dumoria · May 15, 2016 at 08:44 AM 0
Share

I created the variable in the HealthScript, and now you say that I must assign this script in the inspector of the health bar?

avatar image JedBeryll Dumoria · May 15, 2016 at 09:02 AM 0
Share

No, you have the variable in the HealthScript so drag and drop your healthBar gameObject into your HealthScript's variable.

avatar image Dumoria JedBeryll · May 15, 2016 at 09:48 AM 0
Share

The thing is that I can't drag and drop nothing in my HealthScript's variable... No prefab, no gameobject, nothing (as you can see in the photo alt text ) ^^' Did I make a mistake somewhere?

no-variable.jpg (18.2 kB)
Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

i have no clue what is wrong with the script (help) 1 Answer

Annoying Quaternions and Eulers 1 Answer

Why does my start function not work? 1 Answer

How to make low health vignette? 0 Answers

Urgent help with Health System in a 2D game 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