To clarify. When the code below runs, after the coroutine is initiated and the scene is beginning to load the whole process fails when trying to find a correct “portal” (in my code the names of functions and variables have been renamed to protect the innocent) in the loading scene. Take a look.
public class TransitionZone : MonoBehaviour
{
enum ZoneID
{ AN, BN, CN, DN, EN, FN}[SerializeField] int destinationMap = -1; [SerializeField] Transform targetPosition; [SerializeField] ZoneID zoneID; [SerializeField] float length = 1; [SerializeField] float height = 1; [SerializeField] float depth = 1; GameObject player; BoxCollider zone; bool playerInPosition = false; void Awake() { zone = GetComponent<BoxCollider>(); zone.isTrigger = true; zone.size = new Vector3(length, height, depth); player = GameObject.FindWithTag("Player"); } void OnTriggerEnter(Collider other) { if (other.tag == "Player") { Debug.Log(other.name.ToString() + " has entered " + gameObject.name + " Transfer Zone."); StartCoroutine(TransferPlayer()); } } IEnumerator TransferPlayer() { Debug.Log("Couroutine TransferPlayer started."); if (destinationMap < 0) { Debug.LogError("Destination map not selected."); yield break; } DontDestroyOnLoad(gameObject); TransitionZone targetZone = GetTargetZone(); yield return SceneManager.LoadSceneAsync(destinationMap); Debug.Log("TargetZone == " + targetZone.name); Debug.Log("Couroutine TransferPlayer :: targetZone == " + targetZone.name.ToString()); PositionPlayer(targetZone); Debug.Log(player.name + "'s position is " + player.transform.position); Debug.Log("Coroutine executed."); Destroy(this); } void PositionPlayer(TransitionZone targetZone) { Debug.Log(player.name + " initial position is " + player.transform.position); player.GetComponent<IAstarAI>().canMove = false; player.transform.position = targetZone.targetPosition.position; player.transform.rotation = targetZone.targetPosition.rotation; Debug.Log("PositionPlayer :: targetPosition.position = " + targetZone.targetPosition.position); Debug.Log("PositionPlayer :: targetPosition.rotation = " + targetZone.targetPosition.rotation); Debug.Log(player.name.ToString() + " final position is " + player.transform.position); player.GetComponent<IAstarAI>().canMove = true; playerInPosition = true; Debug.Log("PlayerInPosition = " + playerInPosition); } TransitionZone GetTargetZone() { foreach (TransitionZone zone in FindObjectsOfType<TransitionZone>()) { if (zone == this) { Debug.LogError("Same zone selected. " + zone.name); continue; } /* This is where the trouble begins. */ if (zone.zoneID != zoneID) { Debug.LogError("Zone ID conflict. " + zone.zoneID + " " + zoneID); continue; } print(zone.zoneID); return zone; } Debug.LogError("Failed to get a TargetZone."); return null; } void OnDrawGizmos() { Gizmos.color = Color.green; Gizmos.DrawWireCube(transform.position, new Vector3(length, height, depth)); }
}
Here is the Console window:
The scene does load with all assets. No scripts are affected (as to be expected). My script just doesn’t understand the assignment.
What seems to be the problem is that my GetTargetZone blasts through all TransitionZones while the scene is loading then returns null because it has already skipped beyond the intended target. Thus, the player’s unit is never moved from the position it was manually placed by editor, prior to pushing play.
The terrains in use are set at different base heights. so, if you remove the
player.GetComponent().canMove = false;
line, since the current game object never finishes its coroutine it is never destroyed, and our player will either plummet hundreds of units or suddenly appear in the lowest level of Hell (global position). It is amusing… Twice.
Just twice.
If I turn the fellow around and head back to the previous scene we get an encore performance of this behavior, except now we have two undestroyed, lingering game objects / scripts. This can go on as long as you want to tolerate.
I have failed many corrective attempts so far. I have also tried stripping the scene down to just two entrance/egress zones for each map and deactivating all superfluous activities and/or objects. Same behavior, quicker loading time.
I did get excited about a post describing an identical issue by another student. Alas, his clever solution did not satisfy my own frustration.
This is a repost of a currently unsolved mystery. I reposted for clarity and to update information.
If I am posting out of decorum I do apologize. I still be-a-struggling with editing a post on a lecture’s Q&A board.