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 /
avatar image
0
Question by e-bonneville · Apr 26, 2010 at 06:08 PM · javascripthealth-deduction

Problem with health deduction

I've got a problem. I have two scripts, an enemy script and a bullet script. On the bullet script is this function to deduct health from the enemy inside an OnCollisionEnter:

if (collider.gameObject.CompareTag ("Enemy")) {     
    collider.BroadcastMessage("ApplyDamage", 25.0); 
}

I have this script on the enemy to take damage:

function ApplyDamage(damage : float) {  
    hitPoints -= damage;    
    if (hitPoints <= 0.0) {     
        Destroy (gameObject);   
    }    
}

Now, this won't work. Any ideas? Thanks a lot.

EDIT - some additional info:

The enemy is a simple box at the moment with a character controller. It has a gun as a child, without any collider. I have an AI script and a damage reciever script attached to it. When I send it a message to do ApplyDamage from my bullet, nothing happens. The function isn't called. I'm frustrated.

EDIT - some more additional info:

My script works the first time, but then every additional time, it doen't. Almost like it's destroying itself after the first hit. Any more ideas? I've spent a day and a half on this. And yes, I am logging a collision, because my bullet explodes on contact. And, he follows me the first time until I shoot him. Then, he stops moving as well.

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
2
Best Answer

Answer by dhendrix · Apr 26, 2010 at 06:24 PM

Your code is fine as far as I see, so you'll have to do some debugging. Is the enemy receiving the SendMessage at all? If not, then is the collision even happening? How is your enemy structured? As in what is the hierarchy of gameObjects, and which has the collider, rigidbody, and script?

EDIT: Is the console logging an error message along the lines of "SendMessage Function has no receiver!" ? I think the issue is that you aren't logging a collision to begin with. I'm not sure that CharacterControllers send OnCollisionHit messages, but instead OnControllerColliderHit.

Comment
Add comment · Show 3 · 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 e-bonneville · Apr 26, 2010 at 09:28 PM 0
Share

@dhendrix I updated my question above...

avatar image dhendrix · Apr 26, 2010 at 09:42 PM 0
Share

I'm not 100% sure your characterController would log a collision in your example. Try using a Debug.Log statement to check if you are getting a collision.

avatar image e-bonneville · Apr 26, 2010 at 09:56 PM 0
Share

Yeah. That's it exactly. Hmm...

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

No one has followed this question yet.

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Expressions in statements must only be executed for their side-effects? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

What is wrong with my HealthBar/Damage Script? 2 Answers

How can I make my enemy attack me every few seconds? 3 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