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 weedmastersr · Feb 16, 2016 at 01:04 AM · scripting problemshootingbulletdamagehealth

How to make health and damage script function correctly?

Hello everyone,

I'm making a third person shooter. For the shooting I'm using a bullet prefab. I have found some scripts that would apply well to my game but they seem to be malfunctioning. I have this script for the damage, which I have attached to the characters and the player

 using UnityEngine;
 using System.Collections;
 
 public class Damage : MonoBehaviour {
 
     public int health = 100;
 
     // Use this for initialization
     void Start () {
     }
     // Update is called once per frame
     void Update () {
     }
 
     void OnDamage(){
         health--;
         if (health <= 0) {
             Destroy(gameObject);
         }
     }
 }

And I also have this script for the bullet prefab.

 using UnityEngine;
 using System.Collections;
 
 public class MoveBullet : MonoBehaviour {
 
     public float damage = 5.0f;
 
     // Use this for initialization
     void Start () {
 
     }
 
     // Update is called once per frame
     void Update () {
         
     }
     void OnTriggerEnter(Collider Enemy){
         if(Enemy.gameObject.CompareTag("enemy"))
         {
             Enemy.gameObject.SendMessage("OnDamage", damage);
         }
     }
 }

So, the health of the characters is 100. And each hit should be 5 points of damage. However, when I hit play, each bullet only gives me 1 point of damage. Strangely, when I fire a bullet, my character's health also drops by one. So, if I'm hit health drops by one, if I fire health drops by one again.

I actually like the character's health dropping by one when I fire, but why doesn't the bullet give me the specified amount of damage? Which is 5 not 1. I can't understand what I've done wrong with these scrips and why they aren't working properly. Please help! Thanks!

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

3 Replies

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

Answer by ata_2 · Feb 17, 2016 at 11:38 AM

hi move this line from MoveBullet code to Damage

      public float damage = 5.0f;

and make this line change in Damage code from

          health--;

to

          health-=damage;

note: up code like

  health= health - damage; 

if you want you can write it like this

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 weedmastersr · Feb 18, 2016 at 12:28 AM 0
Share

I have followed your instructions but I get the following error: Assets/Scripts/Damage.cs(16,17): error CS0266: Cannot implicitly convert type float' to int'. An explicit conversion exists (are you missing a cast?)

Please help me out with this. Thanks!

avatar image
0

Answer by TreyH · Feb 16, 2016 at 01:17 AM

 void OnDamage(){
          health--;
          if (health <= 0) {
              Destroy(gameObject);
          }
      }

This function just reduces your health by 1, per the 'health--;' line. Small oversight. :-)

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 weedmastersr · Feb 17, 2016 at 12:03 AM 0
Share

Thanks for the reply. It's really helpful. The problem is that if I remove that problematic line, I get parsing errors and the code doesn't work. If I change it to this: void OnDamage (){ health = -damage; if (health <= 0.0) { Destroy(gameObject); } Then I get the error

 Assets/Scripts/Damage.cs(16,27): error CS0103: The name `damage' does not exist in the current context

If I change it this: void OnDamage(float damage){ health = -damage; if (health <= 0.0) { Destroy(gameObject); } I get the error:

 Assets/Scripts/Damage.cs(16,17): error CS0266: Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)

And if I change

 public int health = 100; to 
 public float health = 100.0f; 

Then the game suddenly stop when the first bullet hits me, as if the character died. Although the bullet only carries 5 damage. I get no errors in this instance. How should I proceed to make it work? Also, thanks for the help and sorry for being a noob.

avatar image
0

Answer by Drfox80 · Jul 13, 2020 at 02:59 PM

also, Change in damage to a public void OnDamage(){

Comment
Add comment · Show 2 · 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 Drfox80 · Jul 13, 2020 at 03:15 PM 0
Share

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

public class damge : $$anonymous$$onoBehaviour {

 public int health = 100;

 // Use this for initialization
 void Start()
 {
 }
 // Update is called once per frame
 void Update()
 {
 }

 public void OnDamage(int damge)
 {
     health -= damge;
     if (health <= 0)
     {
         Destroy(gameObject);
     }
 }

}

avatar image Drfox80 · Jul 13, 2020 at 03:16 PM 0
Share

public class $$anonymous$$oveBullet : $$anonymous$$onoBehaviour {

  public float damage = 5.0f;
 
  // Use this for initialization
  void Start () {
 
  }
 
  // Update is called once per frame
  void Update () {
      
  }
  void OnTriggerEnter(Collider Enemy){
      if(Enemy.gameObject.CompareTag("enemy"))
      {
          Enemy.gameObject.Send$$anonymous$$essage("OnDamage", damage);
      }
  }

}

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

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

Health and Damage Script Not Working 2 Answers

Huge Health and Damage Script Problem 0 Answers

Why is this script not working? 2 Answers

Bullet Damage Player's Healthbar,Bullet Damage Other Player's Health 2 Answers

How do I make the object apply damage on trigger? 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