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 17, 2016 at 12:33 AM · scripting problemshootingbulletdamagehealth

Health and Damage Script Not Working

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.

I know the problem line is this one:

    health--;

but I don't know how to fix it. Please explain to me in detail because I'm an idiot. Thanks!

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 ata_2 · Feb 17, 2016 at 11:43 AM 0
Share

please don't send question twice answer past post

avatar image weedmastersr ata_2 · Feb 18, 2016 at 12:29 AM 0
Share

Sorry for sending it twice but I still haven't received any answer that works. 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!

2 Replies

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

Answer by liqvidunity · Feb 17, 2016 at 05:30 AM

In your code Enemy.gameObject.SendMessage("OnDamage", damage); you are passing damage value to OnDamage method.But you are not receiving that value. Kindly update method OnDamage -

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


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:04 AM 0
Share

When I save this code I receive 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?)

avatar image
0

Answer by Zoogyburger · Feb 17, 2016 at 12:58 AM

Not exacly sure about your problem but this worked for me: make sure the player is called player an change your MoveBullet script to this:

 public class MoveBullet : MonoBehaviour {
     public int damageToGive;
 
     void OnCollisionEnter (Collision other)
     {
         if (other.gameObject.name == "Player") 
         {
             other.gameObject.GetComponent<PlayerHealthManager> ().HurtPlayer(damageToGive);
         }
     }
 
 }   
 

You can now change the damage it gives inside the Inspecter Change your Damage script to this:

 public int PlayerMaxHealth;
     public int PlayerCurrentHealth;
     // Use this for initialization
     void Start () {
         PlayerCurrentHealth = PlayerMaxHealth;
     
     }
     
     // Update is called once per frame
     void Update () {
         if(PlayerCurrentHealth <=0)
         {
             gameObject.SetActive (false);
         
         }
     
     }
     public void HurtPlayer(int damageToGive)
     {
         PlayerCurrentHealth -= damageToGive;
     }
     public void SetMaxHeatlh()
     {
         PlayerCurrentHealth = PlayerMaxHealth;
     }
 }
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

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

How to make health and damage script function correctly? 3 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