This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class DestroyingThings : MonoBehaviour | |
{ | |
// This example assumes you will use collision to destroy something. You can use the code inside of OnTriggerEnter2D as well | |
private void OnCollisionEnter2D(Collision2D collision) | |
{ | |
// If the other thing has a specific tag. It's a good idea to limit the detection to specific things | |
if (collision.gameObject.CompareTag("Player") == true) | |
{ | |
// Destroy this gameobject? | |
Destroy(gameObject); | |
// Destroy the gameobject this one collided with? Uncomment this next line | |
//Destroy(collision.gameObject); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here is a bonus Unity example for 2D Game Design F21. Let me know what else you need! | |
====================================================================================== | |
HOW TO DESTROY GAMEOBJECTS | |
Full example: DestroyThings.cs | |
Important: | |
1. Your GameObjects need colliders (set to triggers if necessary) | |
2. The second example assumes you're using OnCollisionEnter2D(Collision2D collision) or OnTriggerEnter2D(Collider2D collision) | |
// Use this to destroy this gameobject: | |
Destroy(gameObject) | |
// Use this to destroy another gameobject upon collision or trigger: | |
Destroy(collision.gameObject); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment