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 dragonCASTjosh · Oct 24, 2013 at 10:13 AM · collisiondamagenot-workinghealth-deduction

health and damage system help

Hi im trying to make a simple health system that will cause the player to lose 10 hp if he walks into a ball. But i cant get it to work, here is my code so far

 var curHealth : int = 50; //set current health
 var maxHealth : int = 100; //set max  health
 var healthPerSec : int = 1; // health regen per second
 var alive : boolean = true; // set the player to alive
 
 //not working 
 function OnCollisionHit(hit :     Collider) {
 
 if (hit.gameObject.CompareTag == "ball"){
     
     curHealth -= 10;
 
 }
 }
 
 
 function Update () {
 
 
  //tets if player should be alive
  if(curHealth <= 0){
      
      alive = false;
      }
 }     
 
 function Start  () {
  
  
     autoHealthRegen();
     
     
 }
  
 // regen health
 function autoHealthRegen () {
  
  
     for(var e=1;e>0;e++) {
  
         yield WaitForSeconds(1);
  
         if(curHealth < maxHealth) {
  
             curHealth= curHealth + healthPerSec;
  
         }
      }
 }
 
  
Comment
Add comment · Show 1
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 vexe · Oct 24, 2013 at 10:15 AM 1
Share

Format your code a bit better please. Did you mean to use OnTriggerEnter(hit : Collider) or OnCollisionEnter(hit : Collision)?

1 Reply

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

Answer by EvilWarren · Oct 24, 2013 at 10:44 AM

This line is wrong

if (hit.gameObject.CompareTag == "ball"){

You are comparing a function with a string, allowed because of UnityScript loose typing.

You want to use either

 if (hit.gameObject.CompareTag("ball")){

or

 if (hit.gameObject.tag == "ball"){

Also, did you check out @vexe's comment above?

Comment
Add comment · Show 22 · 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 vexe · Oct 24, 2013 at 10:52 AM 1
Share

That's cause you're using OnCollisionHit, there's no such thing. See my comment above.

avatar image vexe · Oct 24, 2013 at 11:13 AM 1
Share

collision only works if at least one of your two colliders have a rigidbody. what do the two colliding objects you have attached to them?

avatar image vexe · Oct 24, 2013 at 11:25 AM 1
Share

If this script is attached to a character controller, then you must use OnControllerColliderHit.

avatar image dragonCASTjosh · Oct 24, 2013 at 11:44 AM 1
Share

i have been given this error

 OnControllerColliderHit couldn't be called because the expected parameter Collision doesn't match ControllerColliderHit.
 UnityEngine.CharacterController:$$anonymous$$ove(Vector3)
 Character$$anonymous$$otor:UpdateFunction() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Character$$anonymous$$otor.js:229)
 Character$$anonymous$$otor:FixedUpdate() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Character$$anonymous$$otor.js:331)

code:

 function OnControllerColliderHit(hit : ControllerColliderHit){
 
     if (hit.gameObject.CompareTag("Ball")){
     
         curHealth = curHealth - 5;
 
     }
 }
avatar image vexe · Oct 24, 2013 at 01:48 PM 2
Share

Great, accept the answer above, and upvote any comments that you found useful, since I spent more than 2 hours giving you support. Thanks.

Show more comments

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Enemy deals damage to player on contact 2 Answers

Damage on collision problems... 0 Answers

How Can One Collider Recognize Contact With Another? 0 Answers

Don't restart music on death 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