- Home /
Making a Cube Destroy When Touching Another Cube
I'm using a script that acts as a gravity gun that lets my move around cubes with a rigidbody, I want to make an if statement script with two vars Cube1 and Cube2 that says if cube 1 touches cube 2 destroy cube 1 but I'm still a little new to making scripts in unity and am not sure how to go about doing this.
Answer by ahmedbenlakhdhar · Dec 14, 2014 at 06:07 PM
You may add a new tag (name it "Cube"), attach it to your cubes, and then add the following script to the cubes:
using UnityEngine;
using System.Collections;
public class CubeScript : MonoBehaviour {
void OnCollisionEnter (Collision col) {
if(col.gameObject.tag == "Cube")// "Cube" or whatever you named the new tag
{
Destroy(col.gameObject);
}
}
}
This script destroys the containing game object when it collides with another game object with tag "Cube".
Answer by Tberry · Dec 14, 2014 at 06:23 PM
Thank you this works great. Should have said I prefer Javascript put C# is fine.
You are welcome, but you should use "add new comment" ins$$anonymous$$d of "New answer" if you are not intending to add an answer.
You may mark an answer as "Accepted" if you find it useful.
Check this video for more information about Unity Answers.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Destroy + Score 1 Answer
FindGameObjectsWithTag finds destroyed gameobjects 2 Answers
Trash Collection For Instantiated Game Object 1 Answer
A node in a childnode? 1 Answer