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 sam32x · Jan 23, 2012 at 05:38 AM · damageoncollisionenterhealth

health not subtracting

i made a script for a ship because i wanted to be able to destroy certain parts of it when they take enough damage, except the health variable doesn't go down.

i can see the sparks from the bullets and they are hitting the ship and if i shoot a missile it will explode on the ship but it takes no damage.

here's my script:

 var health = 1000;
 
 function OnCollisionEnter () {
 health -= 50;  //asteroids, missiles and fighters
 }
 
 function ApplyDamage () {
 health -= 1; //bullets
 }
 
 function Update () {
 if(health <= 0){
 Destroy(gameObject); //Destroy the ship when it has no health (no explosion yet)
 }
 }

anyone know the problem?

Comment
Add comment · Show 3
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 syclamoth · Jan 23, 2012 at 05:53 AM 0
Share

Put some Debug.Log in that OnCollisionEnter function, make sure that it is definitely getting called.

avatar image sam32x · Jan 23, 2012 at 07:13 AM 0
Share

well i put Debug.Log in and it got called for the missiles 3 times out of 5 but not for bullets

avatar image sam32x · Jan 23, 2012 at 07:23 AM 0
Share

this is my gun script

 var shotSound: AudioClip;
 var bloodPrefab: GameObject;
 var sparksPrefab: GameObject;
 var loaded = 1;
 var ammo = 0;
 var startammo = 99999999;
 var ableToFire = 1;
 var muzzleflash : GameObject;
 var dElay : float = 0;
 
 function Start() {
 ammo = startammo;
 }
 
 function Update () {
 if (Input.GetButton("Fire1")){
 if (loaded == 1){
 if(ableToFire == 1){
 if(ammo != -1){
         Invoke("Shoot",dElay);
     }
     }
     }
     }
     if (ammo == 0){
     ammo = -1;
     loaded = 0;
     Invoke("reload", 3);
     }
     }
     
     function Shoot(){
     ammo -= 1;
     ableToFire = 0;
     Instantiate(muzzleflash, transform.position, transform.rotation);
     Invoke("rechamber",0.12);
     if (shotSound) audio.PlayOneShot(shotSound);
     var hit: RaycastHit;
     if (Physics.Raycast(transform.position, transform.forward, hit)){
         var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
         if (hit.transform.tag == "Enemy"){
             if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot);
             hit.transform.Send$$anonymous$$essage("ApplyDamage", Send$$anonymous$$essageOptions.DontRequireReceiver);
         } else {
             if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
         }
     }
 }
 
 function reload() {
 loaded = 1;
 ammo = startammo;
 }
 
 function rechamber() {
 ableToFire = 1;
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Jan 23, 2012 at 05:52 AM

That script would work as-is, so there must be some other factor involved. However it's not very efficient to constantly check the health in Update, given that it only changes occasionally. Instead just check when you need to:

 var health = 1000;
 
 function OnCollisionEnter () {
     ChangeHealth (-50); //asteroids, missiles and fighters
 }
 
 function ApplyDamage () {
     ChangeHealth (-1); //bullets
 }
 
 function ChangeHealth (amount : int) {
     health += amount;
     if (health <= 0) {
         Destroy(gameObject); //Destroy the ship when it has no health (no explosion yet)
     }
 }
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trouble setting a parameter with a collision 1 Answer

Health and Damage [C#] 2 Answers

Possible Fall Damage OnCollisionEnter Won't Work! 0 Answers

Need Help With Simple Player Health Damage Script 1 Answer

How do I destroy a game object (The enemy Goblin Game Object) upon entering a collision box? (JavaScript) 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