Destroying an object on collision - c#

I've been trying a way to get rid of an enemy in my game, but once the weapon in the game collides with it it does not do what it's suppose to do. So I tried a different way by turning of the box collider2d, the sprite renderer and another script inside it. But it still does not work.
enter image description here

Just use Destory to destroy the gameObject
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Sword")
{
Destroy(this.gameObject);
}
}

Related

How can I make a particle remove an object?

In my game a player plants a bomb, the bomb explodes creating a fire effect, I would like my fire to be able to kill the player(blue cylinder) and any boxes it collides with. My box and player have colliders. My fire effect is instantiated when the bomb explodes.
How can I make my fire effect destroy my player and box objects?
Can I say something like if fire collider hits player collider, destroy player?
My code for the bomb is as follows
Instantiate(Firebolt, bomb.gameObject.transform.position, Quaternion.identity);
Game Layout
You can add a collider to Firebolt as well and use OnCollisionEnter on it, checking if the object you hit is a player or a box.
This is a simple example that destroys any player or box it collides with:
void OnCollisionEnter(Collision collision) {
GameObject other = collision.gameObject;
// Here I'm using tag to detect if the hit object is a player or a box
// but you can use name or other methods
if (other.tag == 'Player' || other.tag == 'Box') {
Destroy(other);
}
}
check out https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html
you can apply it to the ParticleSystem or to the target GOs that should react to it.
(u also need to activate collision detection on Particle System AND set it to world.

Unity5 OnTriggerEnter2D not called after collision (2D game)

I'm brand new to Unity3D 5 and I'm having issues with my first 2D game involving collision detection. My moving object is a ball and has Rigidbody2D and CircleCollider2D. My stationary "collider or trigger" is a BoxCollider and has a script attached. The OnTriggerEnter2D should be fired when the ball passes through the stationary box. I've also tried OnCollisionEnter2D but I'm fairly certain I should use OnTriggerEnter2D because my stationary box is marked as a trigger.
My code:
public class LoseCollider : MonoBehaviour {
public LevelManager levelManager;
void OnCollisionEnter2D(Collision2D collision)
{
print("collide");
levelManager.LoadLevel("Lose");
}
void OnTriggerEnter2D(Collider2D trigger)
{
print("trigger");
levelManager.LoadLevel("Lose");
}
void OnCollisionEnter(Collision collision)
{
print("collide");
levelManager.LoadLevel("Lose");
}
void OnTriggerEnter(Collider trigger)
{
print("trigger");
levelManager.LoadLevel("Lose");
}
}
As you can see I'm testing all variations and none are being called. Here are my Unity properties for my objects:
And
I'm sure I'm missing something simple if anyone can please point it out to me.
If you are going to to use OnTriggerEnter2D, you must also use a 2D Collider. You are currently using BoxCollider as shown in your second screenshot.
Change that to BoxCollider2D and your Collision callback functions should be called.
I know you already solved it, but I ran into the same problem and found your post. I was using the correct 2D collider, I was missing the Ridgedbody 2D component. It took me a while to find that so I wanted to add it here in case someone else comes across the same problem.

Unity C# OnTrigger/OnCollision Sound

Note: It's a 2D Game
I'm trying to get an audio clip to play when the Character comes into contact with an object that has a Box Collider around it.
I've tried OnTrigger/OnCollision methods but neither are playing any sound. I've also tried many solutions online but still no sound on collision. My Clip works on Awake, but not as intended.
Checklist:
The Player has a "Player" tag.
The Player has a Rigidbody 2D and a Collider 2D
The Oject Collider has 'Is Trigger' ticked
The Object has an AudioSource
The Object has an Audio Script
The Object has a Box Collider 2D for the collision around it
The Audio Clip is attached to the AudioSource component
Here's the current Script (Attached to Object):
I'd be very thankful!
public class Audio : MonoBehaviour
{
public AudioSource audioClip;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
audioClip.Play ();
}
}
}
In order to find out what is wrong, you can start by simplifying the problem.
You can start by doing something simple in OnTriggerEnter2D, that you know will work, such as a Debug.log
void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log(this.gameObject.name+ " collided with "+collision.gameObject.name)
}
This way, you will know if it's the collision or the audio that's the problem.
It may be, that the collision works, but you are unable to hear the audio because of wrong settings or some other minor detail you have missed.

Rigidbody2D to go through Box Collider 2D

I am trying to make a scoring system for a game that I am making; it's a remake of Flappy Bird.
The issue I am having is, I have set up a sprite with a rigidbody2d and a box collider as the score; I am trying to make it so that when the character passes through that sprite; it will add one to the score however when the character attempts to go through it, it just get's pushed back to the other side of the screen.
This is because of the box collider of course but I am not sure of any other way of doing this without this method. Here is the code which detects if the character hits the pipe or the score line:
void OnCollisionEnter2D (Collision2D hit) {
if(hit.transform.gameObject.name == "Pipe(Clone)") {
die();
Debug.Log("hit");
}
if (hit.transform.gameObject.name == "Score(Clone)") {
Debug.Log("Score");
}
}
Everything logs perfectly fine however the problem is that the bird just faceplants into the score line which isn't what is supposed to happen, please try to help or advise what I should do with this.
P.S. I am a novice programmer; I sort of know what I am doing however not really good at debugging things and fixing them, Thank you.
You should check the "Is Trigger" on your Bird object collider in editor. And use:
void OnTriggerEnter2D(Collider2D col) {
if (col.transform.name == "Score")
Score++;
}
In the bird object script.

How do I make it so void OnCollisionEnter() applies to every instantiated object

How do I make it so that this line of code:
void OnCollisionEnter(Collision collision) {
Destroy(gameObject);
}
applies to every instantiated object?
Because only some of them but not all of the instantiated objects destroy themselves.
[PS:] How I want it to be:
Bullet is shot when that bullet collides with another entity it destroys itself and not all of them, I added this PS to be more specific.
If I've understood your question correctly, you want to have your bullets destroy themselves and the object they hit. If so, it would be something like this:
void OnCollisionEnter(Collision collision) {
// destroy the GameObject with which the bullet collided, before destroying the bullet
Destroy(collision.gameObject);
Destroy(gameObject);
}
Your question sounds ambiguous, but I will try my best to answer that.
You can make a separate script for destroying using "OnCollisionEnter" and attach it to your bullet prefab.
Lets say, you created a script called "Destroy" and added following line of code in the class
void OnCollisionEnter(Collision collision) {
Destroy(gameObject);
}
Then attach this script to your prefab called bullet. Now whenever you instantiated a bullet, this script will be attached to that bullet, and as soon as it hits another game object it will destroy itself. If you need to destroy any specific object with that bullet, then you can use tags to do that, something like this
void OnCollisionEnter(Collision collision) {
if(collision.gameobject.tag=="enemy")
Destroy(collision.gameobject); //to destroy enemy
Destroy(gameobject); //to destroy bullet
}
And if you want to destroy all of instantiated gameobject then you shoud store these game objects in array and run a loop into "OnCollisionEnter" to destroy them all together.

Categories

Resources