Trash Collection for Instantiated Objects

Hi. I know I recently posted a question similar to this, but please ignore that one. It did not provide sufficient detail. Here is the new version. Anyways, in my game the player is climbing up infinitely instantiated walls. I want to delete the walls that are no longer needed. Here is the code I have so far:

using UnityEngine;
using System.Collections;

public class BrickWallTrashCollection : MonoBehaviour {

	public GameObject wall;
	public Transform player;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
		if(wall.transform.position = Vector3.Distance(player.position.x + 10, player.position.y +10){
		   Destroy(wall.gameObject);
		}
	}


}

I get an error on the open bracket right after the if statement at the end. It tells me that this was an unexpected symbol. I know there are multiple things wrong with this code so can somebody please point me in the right direction? Thanks!

A few things:

  1. When comparing values, use the == operator, not a single =
  2. You are comparing Vector3 and float values in your if statement, which is not possible.
  3. The wall property you have is already a GameObject, so you don’t need to use wall.gameObject.

Instead try this, where you set a distance value in float units, and the wall will be destroyed when closer than that distance in 3D space. Be sure to stop using the wall reference after you destroy it.

if( Vector3.Distance( wall.transform.position, player.position ) <= SomeDistance )
    Destroy( wall );

Or if you’re specifically comparing X only:

if( wall.transform.position.x  >= player.position.x - 10 || 
    wall.transform.position.x  <= player.position.x + 10 )
    Destroy( wall );