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.

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.