Duplicate Question. Please, make research, and follow the tutorials
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.
Try adding a print(..) to the OnCollisionEnter2D function to see if it's actually being triggered.
Follow this Question
Related Questions
Stuck inside of Composite Collider when using Platform Effector 2D 2 Answers
How to show an object when collided with 0 Answers
Unity Particle Trigger detection 0 Answers
Skip OnTriggerEnter isn't working at all 1 Answer
Masking based on angles 0 Answers