unity freezing to this script

using UnityEngine;
using System.Collections;

public class DeathCubeProcedure : MonoBehaviour {
	public GameObject Target;
	public GameObject Player; 
	// Use this for initialization
	void Start () {

		StartCoroutine ("Procedure"); 

	}
	
	// Update is called once per frame


	IEnumerator Procedure(){
		while (gameObject.activeSelf) {

			for(int i=0; i>4;i++){
				Instantiate(Target,transform.position,Quaternion.identity);
				yield return new WaitForSeconds(1);

			}
			while(transform.position.x!=Player.transform.position.x){
				transform.position=Vector3.Lerp(transform.position,Player.transform.position,Time.deltaTime);
			}

				}
		}

}

i want to know what wrong with my code if that the reason for freezing plus player and target are assigned in the inspector. i also want a solution that is easily to change and keep the first while loop

Your problem is in this loop:

while(transform.position.x!=Player.transform.position.x){
       transform.position=Vector3.Lerp(transform.position,Player.transform.position,Time.deltaTime);
}

So you have two problems. First you don’t have ‘yield’ in this loop, so it will attempt to make the move all in a single frame. Second you are directly comparing two floating poitn numbers in the while condition. Chances are, even with the Lerp(), the two ‘x’ values will never be identical.

There is a third issue. Lerp() used this way takes a very long time to complete. That it, it gets close relatively quickly, but to complete the Lerp() can take far longer than you expect. Replace your while condition with something like this:

while (Vector3.Distance(transform.position, Player.transform.position) > 0.01f)

Then add a ‘yield’ in your loop:

yield return null;