Awake-Funtion Producing Uneven Results
Hi everyone!
I'm working on an RPG-like Door System where the Doors teleport the player not only to a map, but to a position on a map specified by the recipiant door. In Every Scene, there is a Listener that reads the destination of the player and teleports him there, the player itself has "Doesn't Destroy on on Load" attatched, as well as the Game Manager who saves the name of the door and says it to the Scene Setup Script.
This is the Script of the Scene Listener:
public class SceneOverview : MonoBehaviour
{
string DestinationName;
GameObject _Player;
PlayerControlsCC _PC;
Transform[] Doors;
private void Awake()
{
DestinationName = GameManager.Instance._nextDestination;
_Player = GameManager.Instance.Player1;
_PC = GameManager.Instance.Player1Control;
Doors = this.transform.GetChild(0).GetComponentsInChildren<Transform>();
foreach (Transform T in Doors)
{
if (T.gameObject.name == DestinationName)
{
_Player.transform.position = T.GetChild(3).position;
//_PC._controller.Move(T.GetChild(3).position);
Debug.Log("Transition Time, going to Door " + DestinationName);
break;
}
}
_PC.CanMove = true;
}
}
The Scene "awakens", gets the Destination Name from the Game Manager, gets all necessairy player values and the "Door"-Type objects in the scene, stored in children of the Scene Setup object. If the Name of the "Door" aligns with the name of the Destination, the Player is teleported to the position of the fourth child of the door, which is the position the player should go.
However, sometimes, this script doesn't work at all. Even though the Debug Line prints out the correct line, the player is not positioned at the position of the destination object, instead, presumeably still at the global position it was when the old scene got unloaded.
The Player is moved using a character controller and the .move function in FixedUpdate(), if that might contribute something to this problem. Again, most of the time the script works, but sometimes it completely fails, I'm at a loss. I tried calling the Function on Start() and FixedUpdate() instead of Awake() in hopes of overriding any movement of the player by f.e. the script because I suspected that the movement vector is locked in for the next frame even after the scene transitions, because the player locks the position on FixedUpdate(), but I couldn't prove that yet. I'm at a complete loss, so any help would be appreciated.
It's hard to know with certainty what is going on. But, I tend to not rely on other scripts while in the Awake() function. For me, I use Awake to set up the current script I am on, then I use Start() to setup things that rely on other scripts. Doing it this way makes sure that other scripts are also setup correctly - because Awake() happens before any Start() functions.
Beyond that, this one may be hard for us to help track down with what little we have to go off of.
I learned about this when reading up on the actual differences between start and awake. $$anonymous$$y current theory is that the FixedUpdate locks in the $$anonymous$$ovement, the Scene gets loaded, the Player gets teleported, then the FixedUpdate is still not over, it moves the player to the position set with CharacterController.$$anonymous$$ove with the values from the last Input, but still like the player were in the old scene, if that makes sense. I‘ll now try waiting for the FixedUpdate frame to end (somehow?) and then transport that player. Is there any info you need that would help you solve this problem?
Your answer
Follow this Question
Related Questions
LoadScene not working 0 Answers
How Do I Get a Reference To An Object In Another Scene In A Field In The Inspector? 4 Answers
Organising children in the local position is out of place. 2 Answers
Why isn't my start screen loading? 0 Answers
Is there a way to have an Awake() run every time a scene is loaded. 0 Answers