• 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 swiftbolt4 · Aug 15, 2013 at 04:40 AM · javascriptgameobjectvariableoncollisionentersubtracting

How would I be able to subtract from a variable when it hits something? (OnCollisionEnter)

I have been learning Javascript and I was wondering if I could subtract a variable if the game object came in contact with a certain object. For example, if an enemy touched the character, it would say GAME OVER. Something along those lines.

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

Answer by Sajidfarooq · Aug 15, 2013 at 06:08 AM

Yes, its quite simple.

a) Make sure the the character, and the collided object both have colliders attached.

b) Make sure that at-least one of them is a (non-kinematic) rigid-body, generally the character.

Remember that "OnCollisionEnter" will not work with the built-in "CharacterController" that comes with Unity. For that you will need to use "OnControllerColliderHit" instead.

 #pragma strict
 
 var Health:int =10;
 
 function OnCollisionEnter(collision : Collision) 
 {
 
 if(collision.gameObject.tag == "Monster")
 {
     Health = Health-1;
     Debug.Log("Health reduced to " + Health);
 }
     
 if(Health <= 0)
 {
     Debug.Log("Character died");
 }        

}

Comment
Add comment · Show 2 · 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 swiftbolt4 · Aug 15, 2013 at 07:43 PM 0
Share

This is great! Thank you so much! I see now what I was doing wrong. Thanks again!

avatar image Sajidfarooq · Aug 16, 2013 at 04:13 AM 1
Share

Please remember to mark this question as "answered" if you are satisfied with the answer.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 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

Related Questions

How to deactivate something when holding down a key? 1 Answer

GameObject variable change for all of the objects 1 Answer

Acces to a var from another Script. 2 Answers

How do I create an array for multiple targets? 1 Answer

Set script variable to gameObject 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges