• 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 · Oct 17, 2011 at 05:24 AM · something

oncollisionenter nothing is happening

heres my script

 var health = 60;
 var something : GameObject;
 
 function OnCollisionEnter(thing : Collision) {
     if (thing.gameObject.tag == "Bullet") {
     health =- 1;
     }
     else
     if (thing.gameObject.tag == "FlameBall") {
     health =- 3;
     }
     else
     if (thing.gameObject.tag == "Bomb") {
     health =- 6;
     }
     }
     
     function Update () {
     if (health <= 0)
     Destroy (something);
     }

i shot like 20 bombs at the enemy and they were still alive and i could tell they hit because they bounced off

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 sam32x · Oct 17, 2011 at 05:43 AM 0
Share

also the bullets have rigidbodies and the enemy has a box collider and the script is attached, also the bullets say Bullet(Clone)in the hierarchy because i'm cloning them to shoot

3 Replies

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

Answer by save · Oct 17, 2011 at 09:40 AM

A couple of things on the top of my head:

Your script has to be on the enemy (make sure that it is).

Try changing gameObject to collider so it says thing.collider.tag.

To decrease a value by its current you use -= value not =- value, what happens now is that you set the current value to a negative value immediately.

Try an output to the console by Debug.Log(health.ToString()); after you've decreased the health to see what actually happens.

A case switch in this case would be cleaner:

switch (thing.collider.tag) {
    case "Bullet":
        health -= 1;
    break;
}

To destroy a GameObject by its reference from a script attached to it you only need to do this: Destroy(gameObject);

Keep in mind that rigidbodies traveling to fast wont be calculated with precision and you might expect awkward behavior. For bullets you could use a raycast instead of letting the physics engine calculate a rigidbody, for bombs you could use an OverlapSphere and AddExplosionForce.

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 sam32x · Oct 19, 2011 at 07:55 AM 0
Share

i fixed the -=, i changed it to thing.collider.tag, i put Debug.Log(health); in function Update() and health is always at 60

avatar image
0

Answer by henry96 · Oct 17, 2011 at 09:54 AM

I think u should check the tags. Make sure the objects that u wish to have tag have the tag. Also, recheck the capital letter of the tag. It is very sensitive. Check if the tag is capitalized like one written in the script or not. Good luck

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 sam32x · Oct 19, 2011 at 07:54 AM 0
Share

there correct

avatar image
0

Answer by Uniquesone · Oct 19, 2011 at 08:07 AM

isnt it thing.**G**ameObject.tag ? (Uppercase G?)

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 sam32x · Oct 19, 2011 at 10:39 AM 0
Share

yes, but sometimes you have to have gameObject for some random reason

avatar image Uniquesone · Oct 19, 2011 at 11:15 AM 0
Share

Its not a random reason :)

While GameObject is the class, gameObject is the object itselfs.

Uppercase = classes lowercase = vars

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

where do the brackets go? 1 Answer

animation blending with only 1 animation 2 Answers

what do i write here 2 Answers

Trying to replicate a Shader, help? 1 Answer

how to make a gun stay in the same position when you turn the fps controller up 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