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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by noobalert12765 · Oct 26, 2013 at 09:19 PM · collisioncollidersdamagehealth

How to cause damage on collision?

I want it so that when my character walks into a certain object he loses health. Right now when he walks into the object the debug log says hit player, but no health is taken away. Need help fast please. This is the script.

 #pragma strict
 public var damage = 10;
 public var health = 100;
 var healthnow;
 
 function Start () {
 
 }
 
 function Update () {
 
 
 
 }
 function OnCollisionEnter (cool : Collider) {
 if (cool.gameObject.name == "AttackThreshhold") {
         health = health - damage;
         Debug.Log("Hit player");
         
         
 }
 if (health < 0) {
 Application.LoadLevel("Close");
 }
 }
 
 function OnGUI () {
     if (GUI.Button (Rect (10,10,150,100), "Your Health is " + health)) {
         
     }
 }
Comment
Add comment · Show 4
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 Zamaroth · Oct 26, 2013 at 10:25 PM 0
Share

your code seems to be alright... aren´t you accessing health variable from other scripts?

avatar image noobalert12765 · Oct 27, 2013 at 01:36 AM 0
Share

No i was only using this.

avatar image whydoidoit · Oct 27, 2013 at 01:53 AM 0
Share

$$anonymous$$ake sure that the value of damage in the Inspector for this object is set to 10 (not 0 perhaps).

avatar image noobalert12765 · Oct 27, 2013 at 02:32 PM 0
Share

ok so damage is set at 10, but i get this error that says Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jonnyhopper · Oct 27, 2013 at 02:44 PM

Yep so like your message says, OnCollisionEnter has to have an argument of type Collision: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

That collision contains the collider object you need

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 noobalert12765 · Oct 27, 2013 at 03:59 PM 0
Share

Ok, so the error is gone but no health is being taken away from the player. Sorry im just very new to program$$anonymous$$g and all this.

 public var damage = 10;
 public var health = 100;
 function OnGUI () {
     if (GUI.Button (Rect (10,10,150,100), "Your Health is " + health)) {
         
     }
 }
 function OnCollisionEnter () {
 
 
 if (gameObject.name == "Enemy") {
         death();
         
         Debug.Log("Hit player");
         }
         
         
 
 if (health < 0) {
 Application.LoadLevel("Close");
 }
 }
 
 
 
 function death () {
 health = (health - 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

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

weapon collision with characters 1 Answer

Player take damage on collision with AI 1 Answer

Character controller mess up damage 3 Answers

Add Health on Pickup to Decaying Health,Make a collision with an object add health 2 Answers

Take health from enemy 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