- Home /
Destroying Cubes With OnCollisionEnter
Hello everyone,
I'm trying to get an object that moves forward automatically to destroy cubes when it collides with them using the OnCollisionEnter method. Here's my code:
using UnityEngine;
using System.Collections;
public class Destroyer02 : MonoBehaviour {
public AudioClip myClip;
public GameObject otherObj;
// Update is called once per frame
void OnCollisionEnter (Collision collider){
if(otherObj.collider.tag == "Bulls")
{
Destroy(otherObj.gameObject);
AudioSource.PlayClipAtPoint(myClip, transform.position);
}
}
}
RandomUsername this is the code I edited based on your suggestion and I still have the same problem. The object just goes right through the cubes.
Answer by Random username · Oct 06, 2012 at 10:32 AM
Hey, not sure if I'm understanding correctly, but try changing the if statement to be like this: (you should also change the name 'collider' to something like otherObj, because collider is a component of your gameObject)
if(otherObj.collider.tag== "Bulls")
{
Destroy(otherObj.gameObject);
}
Currently you're saying if the object of which this script is attached to has a tag of "Bulls" as opposed to the object it collides with.
Your answer
Follow this Question
Related Questions
How to make gameObject make only one collision 1 Answer
Distribute terrain in zones 3 Answers
Help with destroying objects on collision 2 Answers
I can't get 2DCollider to work. 1 Answer
Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer