Ways to Unity 6: Destroy objects if they get collided
Add Collider
component to your objects
For example, Box Collider
.
Make sure that you click the Is Trigger
selection box to indicate that this object’s collision should call a function called OnTriggerEnter()
.
Add Rigidbody
component to your objects
So that the physical collision would be detected correctly.
Create a new script called DetectCollisions.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DetectCollisions : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other) {
Destroy(gameObject);
Destroy(other);
}
}
Yes, I have noticed that c# in Unity doesn’t have a keyword called
override
. It can cause confusion later when you got a big project.
Who wrote this
https://yingshaoxo.blogspot.com/2020/12/ways-to-unity-6-destroy-objects-if-they.html