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
0
Question by Adrian-Salarda · Jul 03, 2015 at 12:10 AM · enemy damage

My text-based Health isn't working

I've created these line of codes so the enemy script is meant to call the PlayerHealth script. Then the PlayerHealth script is meant to call the text which I've created on a canvas and I've also got a separate script on it called HealthPoints. I can't seem to get them 3 to work and decrease my health bar, i know it's sort of all over the place and but if i can get some help that would be nice. Thank you heaps!

EnemyAttack PlayerHealth playerHealth; bool playerInRange; GameObject player;

 public int attackDamage = 10;
 public GameObject lightEnemy;



 // Use this for initialization
 void Awake () {

     //setting up the references.
     player = GameObject.FindGameObjectWithTag ("Player"); 
     playerHealth = player.GetComponent <PlayerHealth> (); 
 
 }

 void OnCollisionEnter (Collision collision) {

             //if the entering collider is the player... 
             if (collision.gameObject == player) { 

                     //...the player is in range. 
                     playerInRange = true;
 
             } 

     } 

 void OnCollisionExit (Collision collision) { 

     //if the exiting collider is the player...
     if (collision.gameObject == player) { 

     //...the player is no longer in range. 
         playerInRange = false;

             }

     }
 
 // Update is called once per frame
 void Attack () {

     //if the player has health to lose... 
     if (playerHealth.currentHealth > 0) { 

     //...damage the player. 
     playerHealth.TakeDamage (attackDamage);
         player.GetComponent<PlayerHealth> (); 

             }

 }
 

}

PlayerHealth public GameObject player; public int startingHealth = 50;
public int currentHealth;
bool damaged; bool isDead;

 public Text healthPoints; 
 
 void Awake () {

             currentHealth = startingHealth;
     } 
 
 // Update is called once per frame
 public void TakeDamage (int amount) { 

     //reduce the current health by the damage amount.
     currentHealth -= amount; 

     //set the health point's value to the current health. 
     healthPoints.text = "" + currentHealth; 

     //if the player has lost all its health and death hasn't been called yet...
     if(currentHealth <= 0 && !isDead) {

         Death (); 

     } 

 } 

 void Death () {  

     //if the bool is true...
     if(isDead = true) { 

         //...then load level.
         Application.LoadLevel (3); 

     } 

 } 

}

HealthPoints public int maxHealth; public static int currentPlayerHealth;

 Text text;

 // Use this for initialization
 void Start () { 
     text = GetComponent<Text> (); 
     currentPlayerHealth = maxHealth;
 
 } 

 // Update is called once per frame
 void Update () { 
     if (currentPlayerHealth <= 0) { 
         currentPlayerHealth = 0;

         } 

     text.text = "" + currentPlayerHealth; 

 }

}

Comment
Add comment · Show 2
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 meat5000 ♦ · Jul 02, 2015 at 03:48 PM 1
Share

Theres no question here.

Be specific.

avatar image zaid87 · Jul 03, 2015 at 02:32 AM 0
Share

Just to be sure; are all the script running (are they attached to a game object inside the scene)? I see the connection between the first 2 scripts, but there's nothing that seems to be updating currentPlayerHealth inside Health script.

You seem to update the text in PlayerHealth but set it back in HealthPoints Update(). Could that be the case?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by superpentil · Jul 03, 2015 at 07:39 AM

If your GUI is having trouble getting values, there is a function on int called ToString() that returns a string value of your int. So for places where you set text you should set it this way:

 text.text = currentPlayerHealth.ToString();

I haven't used this with floats or doubles but I don't see why ToString() wouldn't be available for those if you ever need them.

As for potential problems, you seem to have multiple scripts that do the same thing and they may be interfering with each other (i.e. HealthPoints and PlayerHealth). You should instead think about all the functions that correspond to player health and keep them in one script. I'd get rid of HealthPoints and consolidate whatever it is you're trying to do to PlayerHealth:

 public GameObject player;
 public int startingHealth = 50;
 public int currentHealth;
 public Text text;
 bool damaged;
 bool isDead;
 
 void Start()
 {
     isDead = false;
     isDamaged = false;
     currentHealth = startingHealth;
 }
 
 void Update()
 {
     // In your code you have a typo of only one ‘=‘. One means you’re assigning,
     // two means you’re comparing values. The only time you’d put one is when you
     // have another comparison sign along with it like below.
     // I’d put isDead check here for the same reason I state with text.text below.
 
     if(currentHealth <= 0)
     {
         isDead = true;
     }
 
     if(isDead == true)
     {
         //Can have Death() here or just
         Application.LoadLevel(3);
     }
 
     // You can do this every time update is called so it’s “precise”,
     // But you can also put it in your TakeDamage function.
     text.text = currentHealth.ToString();
 }
 
 public void TakeDamage(int amount)
 {
     // Since isDead is only true when we hit 0 on currentHealth, we can use this
     // to double check whether we can take damage or not.
     if(isDead == false)
     {
         currentHealth -= amount;
     }
 }

In your EnemyAttack script, things look ok but I have no idea why you're calling player.GetComponent<PlayerHealth>(); It doesn't look like it does anything. You already have a reference to PlayerHealth at the start with the PlayerHealth playerHealth variable. All you have to do is use it once here:

 // Your EnemyAttack script summed up.
 PlayerHealth playerHealth;
 GameObject player;
 
 void Start()
 {
     player = GameObject.FindGameObjectWithTag(“Player”);
     playerHealth = player.GetComponent<PlayerHealth>();
 }
 
 // This is just a note for you…
 void OnCollisionEnter(Collision collision)
 {
     // gameObject already has a tag variable you can use.
     // You don’t need to use collision.gameObject == player.
     if(collision.gameObject.tag == “Player”)
     {
         playerInRange = true;
     }
 }
 
 void Attack()
 {
     // Notice no if statement here because it’s already handled by the PlayerHealth
     // script.
     playerHealth.TakeDamage(attackDamage);
 }

I have no idea why you broke everything up when you didn't need to.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Scrolling space shooter - making enemies drop currency 1 Answer

How to make enemy shoot at player 0 Answers

I am making a FPS and I was wondering how i could make it so when the enemy gets close enough he starts to deal damage. 0 Answers

why isn't my enemy shooting at me? 0 Answers

Bullet Interaction 2 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