Player is teleported to the default scene placement

Hello !
yield return SceneManager.LoadSceneAsync(sceneToLoad); seems to not waiting the end of the loading scene. When going to my level 2, the player is first spawning to my spawnPoint and is then teleported to the default scene position.
For now i’m using yield return new WaitForSeconds(0.1f); after so it’s working.
What i really don’t understand is that it’s only bugging when going from level 1 to level 2 but not from level 2 to level 1. I really can’t get it.

1 Like

Are there any error messages in the console?

Thanks for your answer. No i don’t have any error in the console.

I commented the yield return new waitForSeconds() so there is still the bug

Usually, this sort of issue is caused by the Portal not being in the root of the hierarchy (in which case with or without the WaitForSeconds, nothing would happen, the player would never move from the default starting point). In that case there would be a warning (should be an error). I suspected this at first, because you mentioned that it only occurrs one way. I’m not sure why the WaitForSeconds() factors in.

Can you post your UpdatePlayer() method?
When posting scripts, instead of a screenshot, on it’s own line type the backwards apostrophe three times ```, then on a new line paste in the text of the script, followed by another ``` (That’s the apostrophy next to the 1 on your keyboard).

Thanks for the tip, i didn’t know that.
So here’s my UpdatePlayer() :

        void UpdatePlayer(Portal otherPortal)
        {
            GameObject player = GameObject.FindWithTag("Player");
            {
                player.GetComponent<NavMeshAgent>().enabled = false;
                player.transform.position = otherPortal.spawnPoint.position;
                player.transform.rotation = otherPortal.spawnPoint.rotation;
                player.GetComponent<NavMeshAgent>().enabled = true;
            }
        }

I first used warp() but it was the same issue.
It seems to be an obscure problem i strictly followed the course and didn’t make anything fancy in the scene. i can clearly see that there is 2 teleportation one is correct and the other that put me at the default scene position

Hmmm… I’m at a loss on this one. Since the yield return new WaitForSeconds() is working, that might be the solution for the time being. My best guess is that somehow the LoadSceneAsync is returning before everybody has ran their Awake() method, which should not be happening. The Wait() lets the characters Awake() routines to run (which includes things like setting positions based on serialized data).
You should actually be able to replace that line with

yield return null;

Which would have the effect of waiting one frame rather than a 10th of a second, and at that point everything should have their Awake() methods ran.

So i found something interesting, if i’m printing something in the console at awake on a empty in the scene, the message appear 2 time which mean that the scene is loaded 2 times i think. I will investigate this way

Ok ! So i Found it, i’m an idiot so no problem ! :smiley: I had two capsule collider on my player so onTriggerEnter was called twice.
Thanks for your help anyway :slight_smile:

1 Like

Wow, I hadn’t thought of that. Extra components can definitely cause difficulty. You haven’t gotten to the saving system yet, but a common problem is when you have two Health components on the player and the first one is the one used by the game, but the second one overwrites the first one in the save file.
Good job finding that!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms