• 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 Apr 05, 2016 at 08:00 AM by Hellium for the following reason:

Duplicate Question. Please, make research, and follow the tutorials

http://unity3d.com/learn/tutorials/topics/physics

avatar image
0
Question by RealcakeTheBoss · Apr 05, 2016 at 07:59 AM · c#unity 52dcollision

Can't get object to destroy itself on collision.

I am trying to get a bullet object destroy itself on collision, in a 2D game. I read a page about collision by Unity and copied the code in, but I didn't get any results. My bullet object has a Rigidbody 2D with Continuous collision detention. I made sure everything was on the same Z coordinates and layer. Here is the coed for my bullet object.

using UnityEngine; using System.Collections;

public class Move_Trail : MonoBehaviour {

 public float moveSpeed = 5;
 
 void start()
 {
 }
 // Update is called once per frame
 void Update () {
     transform.Translate(Vector3.right * (Time.deltaTime ) * moveSpeed);
     Destroy(gameObject, 1);
 }

 void OnCollisionEnter2D(Collision2D coll)
 {
     Destroy(this.gameObject);
 }

 void OnCollisionEnter(Collision collisionInfo)
 {
     Destroy(this.gameObject);
     print("Detected collision between " + gameObject.name + " and " + collisionInfo.collider.name);
     print("There are " + collisionInfo.contacts.Length + " point(s) of contacts");
     print("Their relative velocity is " + collisionInfo.relativeVelocity);
 }

 void OnCollisionStay(Collision collisionInfo)
 {
     Destroy(this.gameObject);
     print(gameObject.name + " and " + collisionInfo.collider.name + " are still colliding");
 }

 void OnCollisionExit(Collision collisionInfo)
 {
     Destroy(this.gameObject);
     print(gameObject.name + " and " + collisionInfo.collider.name + " are no longer colliding");
 }


 void OnTriggerEnter(Collider other)
 {
     Destroy(this.gameObject);
     print("Collision detected with trigger object " + other.name);
 }

 void OnTriggerStay(Collider other)
 {
     Destroy(this.gameObject);
     print("Still colliding with trigger object " + other.name);
 }

 void OnTriggerExit(Collider other)
 {
     Destroy(this.gameObject);
     print(gameObject.name + " and trigger object " + other.name + " are no longer colliding");
 }

}

At this point, I am all out of ideas.

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 ShadoX · Apr 05, 2016 at 08:01 AM 0
Share

Try adding a print(..) to the OnCollisionEnter2D function to see if it's actually being triggered.

0 Replies

  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Stuck inside of Composite Collider when using Platform Effector 2D 2 Answers

Creating dynamically moving buttons/GUI objects. 1 Answer

Unity 2D Touch drag specific Object 0 Answers

Detect the objects staying on top of the Particles 0 Answers

Trouble with directing launched projectiles in Unity 2D 0 Answers

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