Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
This question was closed Feb 29, 2016 at 08:51 PM by Son_ofSpartacus for the following reason:

The question is answered, apparently, you need '2d' after 'OnCollisionEnter' and 'collision' in order for it to work in 2d. Who would've thought.

avatar image
0
Question by Son_ofSpartacus · Feb 28, 2016 at 09:45 AM · 2dscripting problemcollisiondestroy

C# OnCollisionEnter not Triggering If Statement

So, I'm using a game object with a box collider 2d for a bullet. The script below is supposed to say that if the bullet object collides with the enemy the script is attached to, then it's health is subtracted by 1, the bullet object is destroyed, and a debug message is displayed. This doesn't happen, and instead the bullet pushes the enemy and continues on it's merry way. The script for the bullet literally just says to be moving up at the angle in instantiates at, in case you wanted to know. So, why isn't the bullet corresponding to any of this script?

 using UnityEngine;
 using System.Collections;
 
 public class EnemyHealthAndDamage : MonoBehaviour
 {
 
     public float Health = 3f;
     public float maxHealth = 3f;
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if (Health > maxHealth)
         {
             Health = maxHealth;
         }
 
         if (Health <= 0)
         {
             Destroy(gameObject);
         }
 
     }
 
     void OncollisionEnter(Collider other)
     {
         if (other.gameObject.name == "PlayerBullet(Clone)") 
         {
             Destroy(other.gameObject);
             Health = Health - 1;
             Debug.Log("Hey it worked");
         }
     }
 
 }

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

  • Sort: 
avatar image
1

Answer by tanoshimi · Feb 28, 2016 at 09:46 AM

OnCollisionEnter not OncollisionEnter.

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 Son_ofSpartacus · Feb 29, 2016 at 07:17 AM 0
Share

This didn't work. Thanks for the correction on that though. I wonder if the script has to be on the bullet object and not the enemy to work?

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnTriggerEnter getting called more than once 0 Answers

How to check if class has RequireComponent on it? 1 Answer

Why Isn't My OnCollisionEnter2D Code Not Working? 1 Answer

distinguish players for collision between them 0 Answers

It seems my tag detection does not work properly 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