Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
2
Question by Digital-Phantom · Feb 26, 2015 at 05:06 PM · c#gameobjectcolliderontriggerenterridigbody

OnTriggerEnter not recognising collision ?(Solved)

I have two objects, a cannonball and a target. For some reason (probably something minor that I'm just not seeing) OnTriggerEnter just isn't recognising when the two collide.

The target has a box collider with 'is trigger' checked. The cannonball has a ridigbody and sphere collider.

This is the script -

 using UnityEngine;
 using System.Collections;
 
 public class MegonHealth : MonoBehaviour
 {
 
     public static int megonLife = 4;
     public Transform bossExplosion;
     
 
     void OnTriggerEnter(Collider collider)
     { 
         if(collider.gameObject.name == "CannonBall")
         {
             megonLife -= 1;
             Instantiate(bossExplosion, transform.position, transform.rotation); // Instantiate Explosion Effect
         }
     }
 }
 
 

What am I missing guys ???

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

3 Replies

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

Answer by psykojello2 · Feb 26, 2015 at 06:52 PM

Try to give both your objects a rigidbody. You can check "isKinematic" on the rigidbody if you don't want it to be affected by physics.

I'm assuming both objects have a collider as well.

See if either object is on a particular layer. If they are, then check Project Settings->Physics and make sure that the two layers are able to collide in the Collision Matrix.

Hope that helps.

Oh, also put a debug.log statement inside your onTriggerEnter function before you check for cannonball. Something like "Debug.Log("Collided with "+other.gameobject.name");" just to check if you're colliding with anything at all.

Edit: Solution: Check the game object's tag instead of name. Set a tag for your cannon ball called "CannonBall" and check it in your OnTriggerEnter function. if(collider.gameObject.tag == "CannonBall") DoStuff()

Comment
Add comment · Show 8 · 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 psykojello2 · Feb 26, 2015 at 06:54 PM 2
Share

If you're working on a 2D game, be sure to use OnTriggerEnter2D, Collider2D and RigidBody2D

avatar image Digital-Phantom · Feb 26, 2015 at 07:28 PM 0
Share

ok I edited the OnTriggerEnter event to do a debug test -

 void OnTriggerEnter(Collider collider)
     { 
         Debug.Log("Collision Detected");
 
         /*
         if(collider.gameObject.name == "CannonBall")
         {
             megonLife -= 1;
             Instantiate(bossExplosion, transform.position, transform.rotation); // Instantiate Explosion Effect
         }
         */
     }
 

Getting a message every time, so the trigger is obviously recognising the collision.

The spelling and the capitalisation of the CannonBall are exact.

Both the trigger object and the other object are on the same layer.

???

avatar image psykojello2 · Feb 26, 2015 at 07:44 PM 1
Share

Add the name of the collided object to see what it's looking at:

Debug.Log("Collision Detected "+collider.gameObject.name);

avatar image Digital-Phantom · Feb 26, 2015 at 07:49 PM 0
Share

get this message -

Collision Detected CannonBall(Clone) UnityEngine.Debug:Log(Object) $$anonymous$$egonHealth:OnTriggerEnter(Collider) (at Assets/$$anonymous$$egonHealth.cs:13)

avatar image psykojello2 · Feb 26, 2015 at 10:03 PM 1
Share

There's your answer right there :) The object's name is CannonBall(Clone) not CannonBall. This is happening because you're instantiating the CannonBall in your scene.

Ins$$anonymous$$d of checking for the gameObject's name, you should check for its tag. Set a tag on your cannonBall prefab called "CannonBall".

Then check your collision for :

 if(collider.gameObject.tag == "CannonBall")
 {
     //your logic here. Boom!
 }

To add a tag, click the drop down under the GameObject's name in the inspector and choose "Add Tag..." and create a new one. Don't forgot to set the tag after creating it. Good luck!

Show more comments
avatar image
0

Answer by MathieuBarbier · Feb 26, 2015 at 05:18 PM

Did you check frame by frame that your colliders actually collide. Sometimes if your object is moving very fast it can miss the collision.

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 X10KND · Feb 26, 2015 at 05:59 PM 0
Share

i agree. it can happen

avatar image
0

Answer by X10KND · Feb 26, 2015 at 05:40 PM

i'm not sure but i guess your actual gameobject name did not match with the on in the script (script line 13). please reply if the problem is solved

Comment
Add comment · 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

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

23 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Trigger when 3 triggers collide. 1 Answer

Collision Detection without a RigidBody 2 Answers

Problems with the triggers(not recognized) 1 Answer

Collision Checking 1 Answer

Adding Force to a Rigidbody through OnTriggerEnter 1 Answer


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