If xGameobject collides with (Gameobject with label) Destroy it.

As the title implies I need help coding this. I’m working on a breakout-style game where xGameobject is a ball that will collide with other gameobjects under a certian label “wall”. What am I doing wrong? there are no errors, but the walls won’t destroy…

using UnityEngine;
using System.Collections;

public class Wall : MonoBehaviour {


	private GameObject xGameObject;
	
	void OnCollisionEnter(Collision col)
	{
		if(col.transform.tag == "Wall")
		{
			Destroy(xGameObject);
		}
	}
}

is this script attached to the ball or walls?

If you attach the following code to your ball then this should work

void OnCollisionEnter (Collision col)
	{
		if (col.transform.tag == "Wall") {
			Destroy (col.gameObject);
		}
	}

Hmmm… Your script is named “Wall”, so I assume it is attached to the wall objects? If so, then it’s no wonder it doesn’t work since no wall object will ever register a collision with another wall object :slight_smile: A good example of a logic error.

What you need to do is put the script on the Ball object instead.

As another tip: Insert Debug.Log lines in your code to print information in your console during runtime; For example, you can use

Debug.Log(gameObject.name + " has registered a collision with " + col.gameObject.name);

in your OnCollisionEnter function.