• 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
Question by Infamous911 · Jul 25, 2011 at 08:18 PM · c#collisioncollidertriggerontriggerenter

Collision problem in C#

So basically I want to be able to detect if my capsule collides with my sphere. Right now it just isn't working. Here is the script I place on the sphere:

 public GameObject ParticlePrefab;
 
 void OnTriggerEnter(Collider col){
     Debug.Log("Hit");
     Destroy(col.gameObject);
     Instantiate(ParticlePrefab, transform.position, transform.rotation);
 }

For some reason it never does any of this when the capsule collides with it. I have the capsule as a Rigidbody and a trigger, so I can't think of a reason why this won't work. Any help would be appreciated.

Comment

People who like this

0 Show 0
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

4 Replies

  • Sort: 
avatar image
Best Answer

Answer by Infamous911 · Jul 26, 2011 at 07:43 PM

Ok the problem was that I never actually applied the rigidbody properly. Thanks for the help!

Comment

People who like this

0 Show 0 · 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

Answer by Giometric · Jul 25, 2011 at 08:49 PM

Is the Sphere that this script is on also a trigger?

edit: actually the example code for C# does have some stuff not present in your script.

 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour {
     void OnTriggerEnter(Collider other) {
         Destroy(other.gameObject);
     }
 }

I'm not a C# user myself but it might just need those first few extra lines and also the "public class example" part before it will work at all.

Comment

People who like this

0 Show 3 · 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 Alec-Slayden · Jul 25, 2011 at 08:53 PM 0
Share

I think as long as one of them is a trigger, OnTriggerEnter() gets called on both.

avatar image Giometric · Jul 25, 2011 at 08:55 PM 0
Share

Yeah, I remembered that right after posting hehe, quickly edited with a better answer :P

avatar image Infamous911 · Jul 25, 2011 at 09:41 PM 0
Share

Yes I have that, I just edited out all of the other stuff so that you could see the script that was associated with the problem.

avatar image

Answer by Infamous911 · Jul 25, 2011 at 08:52 PM

No but I tried that and it doesn't change anything.

Comment

People who like this

0 Show 0 · 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

Answer by Giometric · Jul 25, 2011 at 09:53 PM

Ok I went ahead and replicated the scene you described, and it seems to be working fine. This is what the entire script I have looks like:

 using UnityEngine;
 using System.Collections;
 
 public class test2 : MonoBehaviour {
     
     public GameObject ParticlePrefab;
 
     void OnTriggerEnter(Collider col){
         Debug.Log("Hit");
         Destroy(col.gameObject);
         //Instantiate(ParticlePrefab, transform.position, transform.rotation);
     }
 }

Though as you can see I commented out the Instantiate part, the Debug.Log part is definitely working. So I'm not sure what's going on with yours.. my scene is using a simple Unity sphere and capsule, the sphere has the script on it, and the capsule has a rigidbody applied and nothing else. Are you using a character controller on your capsule?

Comment

People who like this

0 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 Infamous911 · Jul 25, 2011 at 10:23 PM 0
Share

Can you show me how you're applying the Rigidbody? Maybe that has something to do with it.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why the npc character walking strange when using a Rigidbody and Is Kinematic on enabled ? 1 Answer

On Trigger Enter, Collide with object, specific collision 1 Answer

How to enter trigger a certain amount of times before executing code? 1 Answer

Trigger Spawning? 1 Answer

Keep doing something while colliding 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