Teleport stops udating after 4-5 teleportations

I have this wierd problem with my teleportsystem, that i just cant debug on my own.

I have a player, which has the scripts PlayerMovement and PlayerTeleport attached.
I currently have it working so that when i click on an object tagged “Teleport” and i am within “UseRange” then a GUI-window shows up with 3 different teleport destinations. When i click on one (which is not the one the player is currently standing on), the PlayerTeleport-script accesses a method in PlayerMovement called TeleportPlayer, which takes care of changing the position of the player to the position of the chosen teleport destination. This works perfectly fine the first time i teleport. If i then teleport to a new destination, the player is teleported, but the GUI doesnt show up by it self. If i then rightclick on the teleporter, the GUI shows, and the player is once again able to teleport - but the 4. or sometimes 5. time (seems kinda random) the position of the player will change (the player gets teleported) but the PlayerTeleport-script is now unable to update (it has the positions of all teleports stored and calculates which teleporter is the nearest relative to the transform.position of the player).

The PlayerTeleport-script is as following:

using UnityEngine;
using System.Collections;

public class PlayerTeleport : MonoBehaviour {
	string HitTag;
	float UseRange;
	Vector3 PlayerPosition;
	Vector3 SpawnTeleportLocation;
	Vector3 GraveyardTeleportLocation;
	Vector3 GardenTeleportLocation;
	GameObject[] TeleportTags;
	GameObject NearestTeleport;
	float TeleportDistance = Mathf.Infinity;
	float TeleportNear;

	void Start () {
		SpawnTeleportLocation = GameObject.Find("SpawnTeleport").transform.position;
		GraveyardTeleportLocation = GameObject.Find("GraveyardTeleport").transform.position;
		GardenTeleportLocation = GameObject.Find("GardenTeleport").transform.position;
		TeleportTags = GameObject.FindGameObjectsWithTag("Teleport");
		UseRange = GetComponent<PlayerStats> ().UseRange;
	}
	void Update () {
		HitTag = GetComponent<ClickHover> ().HitTag; //Finds out whether the last right-clicked object is taget "Teleport"
			if (HitTag == "Teleport") {
				FindNearestTeleport ();
			}
			if (HitTag == "Teleport" && Vector3.Distance(NearestTeleport.transform.position, PlayerPosition) <= UseRange){
				ShowTeleportGUI = true;	
			} else {
				ShowTeleportGUI = false;
			}
	}

		GameObject FindNearestTeleport(){
			PlayerPosition = GetComponent<PlayerMovement> ().CurrentPlayerPosition;
			foreach (GameObject Teleport in TeleportTags) {
			TeleportNear = (Teleport.transform.position - PlayerPosition).sqrMagnitude;
				if (TeleportNear < TeleportDistance) {
					TeleportDistance = TeleportNear;
					NearestTeleport = Teleport;
				}
			}
			return NearestTeleport;
		}

The PlayerMovement-script is as following:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

	public float PlayerMovementSpeed = 55f;
	public float PlayerRotateSpeed = 10f;
	public CharacterController PlayerController;
	public Vector3 PlayerPosition;
	public Vector3 CurrentPlayerPosition;
	public Quaternion CurrentPlayerRotation;

	void Start () 
	{
		PlayerPosition = transform.position;
		if (PlayerPosition.y > 0f)
				{
					RaycastHit hit;
					if (Physics.Raycast (PlayerPosition, -Vector3.up, out hit)) 
						{
							float DistToGround = hit.distance;
							PlayerPosition.y = PlayerPosition.y - DistToGround;
							Debug.Log(DistToGround);
						}
				}
	}

	void Update () {
		if (Input.GetMouseButton (1)) {
			LocatePosition ();
				}

			MoveToPosition ();

			CurrentPlayerPosition = transform.position;
			CurrentPlayerRotation = transform.rotation;
		}
	void LocatePosition()
		{
			RaycastHit hit;
			Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			if (Physics.Raycast (ray, out hit, 10000)) 
			{
				PlayerPosition = new Vector3(hit.point.x, hit.point.y, hit.point.z);
			}

		}

	void MoveToPosition()
		{
		if (Vector3.Distance (transform.position, PlayerPosition) > 1f) {
			Quaternion newRotation = Quaternion.LookRotation (PlayerPosition - transform.position);

			newRotation.x = 0f;
			newRotation.z = 0f;

			transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * PlayerRotateSpeed);
			PlayerController.SimpleMove(transform.forward * PlayerMovementSpeed);

		} 

	}
	public IEnumerator TeleportPlayer (Vector3 TeleportDestination){
		// Teleport
		transform.position = TeleportDestination;
		PlayerPosition = TeleportDestination;
		CurrentPlayerPosition = TeleportDestination;
	}

	
}

There is never any errors during gameplay, but inserting varius Debug.Log’s different places in the scripts indicates that for some reason the FindNearestTeleport(); stops updating the playerposition after 4-5 uses of the teleports, and therefor makes it unable to calculate the true distance between the player and the teleports.
If for example i stand on the spawn teleporter and teleports to the garden teleporter, then the player will get teleported, but FindNearestTeleport(); still recognizes the spawnteleporter as the nearest teleporter. And if i walk back to the spawn teleporter at this point, the player is able to teleport again, but it still wont update properly.

Also as a sidenote, the reason for the TeleportPlayer(); to be an IENumerator is because it also takes care of some particles and a small waittime before the teleport takes place, but i have tested with all this code being commented out which didnt solve the problem :slight_smile: So im not expecting this to be the reason for the problem to persist :slight_smile:

Let me know if i need to explain some of the scripting further :slight_smile: Also - very long post, i know, so any help is very much appreciated! :slight_smile:

You never reset your TeleportDistance variable back to infinity before using it in the FindNearestTeleport() foreach loop, so it compares the value and fails when you stand farther from your teleport than before.