Destroy clones when colliding with a wall

I’m currently working on a game that spawns obstacles ( walls ) coming towards you, and I want those walls (spawned clones) to be destroyed when entering the wall behind the player.

I looked around for a bit, people said to use this:

void onCollisionEnter(Collision colliderHit)
		if ( == "destroyer") { //destroyer = wall behind player, wanted to use as a Collider for deleting

			Destroy (colliderHit.gameObject);

But it doesn’t work for me, the wall just continues to travel away into the wasteland.

Someone said the instances both needed to be rigidbody, which is impossible for the situation because the moving walls go through other walls, as rigidbody they’d get stuck.

I don’t trust collision testing for things like this. It’s more reliable and cheaper to just set up a game object or set of game objects for your boundary locations (I typically use a min and max, so 2 game objects, at opposite corners to define a 3D cube of play area), or you can even use the existing wall and get the extents of the mesh shape in your script Start method, and reference that object by some Singleton manager class with an exposed method like “public static bool IsInPlayArea(GameObject objectToTest)”. Then test each frame if the position of the moving wall is beyond the position of the static “boundary” walls (the wall behind your player) or any other walls you care about. It’s not going to be as exact of course, but it generally solves most problems.

The problem with using collision detection in situations like this is that your frame may have a blip of high frame time, and one frame the moving wall may be in front of the “boundary” wall and be behind it in the next frame, completely missing the collision, depending on the speed, the device, the operating system. Then you overcompensate by making the back wall really deep or something, which is fine. But the biggest improvement is saving performance on collision testing between the moving wall (and everything else in the scene) with the static boundary wall.

In your code above, either way, the Destroy() is destroying the boundary wall, not the moving wall, assuming the script above is placed on your moving wall. If the code above is placed on your boundary wall, and your moving wall is tagged “destroyer” (based on the comments I assume this is not the case), then it should work, and is probably due to the lower case “O” in onCollisionEnter as pointed out in another answer.

Try this

Remember tag your Destroyer with “Destroyer”.

void OnTriggerEnter(Collider other)
		if (other.gameObject.tag == "Destroyer") 
			other.gameObject.SetActive (false);
			Destroy (other.gameObject, 2f); //this helps destroying object in 2 sec

Hope this works.

capital “O”.

Use oncollisionEnter and the object name will be something like

  Destroy (other.gameObject(Clone), 2f)

i didn’t try this but give it a go.