Problem in position

hi all,
i came out a concept which got a moving game object which only moves forward with certain speed with control of left and right and jump,

i wish to destroy a travelled objects when reaching a certain distances whereas spawn another object in front with a certain distances too.

i try to use

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour 
{
	private GameObject destroy = GameObject.FindWithTag("Track");
	// Use this for initialization
	void Start () 
	{
		transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
		destroy.transform.position = new Vector3(transform.position.x, transform.position.y,-52.64419f);
	}
	
	// Update is called once per frame
	void Update () 
	{
		if((transform.position.z - destroy.transform.position) > 27)
			DestroyImmediate(destroy,true);
	}
}

to destroy, but it is not working…i need some helps…thanks…

Something is wrong logically or syntactically in your Update, you’re subtracting “transform.position.z - destroy.transform.position”, while you probably should check for Vector3.Distance between the two positions