I’ll try to explain the specifics, while being as concise as possible.
When the player loads into the level, there are a bunch of different spawn points where they can spawn. The choice of spawn points is saved into a data file in the main menu scene, then the actual level scene loads the datafile, grabbing all the pertinent information out of it (including the spawn point chosen in the main menu).
The key code is simply:
player.transform.position = GameObject.Find("PlayerSpawns").transform.Find(startPos).transform.position;
player.transform.rotation = GameObject.Find("PlayerSpawns").transform.Find(startPos).transform.rotation;
startPos is a string that reads in from the datafile.
The problem is that sometimes the player is simply not moved and spawns in at the start location where the player GameObject is set in the editor. Only sometimes. Sometimes the first time testing, sometimes the 19th time, sometimes the 4th through the 7th…I have not been able to detect any pattern. There are 8 possible start locations and all of them work most of the time (and even hard coding to a specific one doesn’t make a difference).
I’ve logged the startPos and it is always valid and matches the names of the empty GameObjects marking the start positions. I’ve grabbed the start position GameObjects and transforms to make sure they were valid and held accurate data (they were fine). I’ve logged right before and after these lines to confirm that they are always running, and they are running when it fails to set the position. I even thought that the problem might be something going on in the Start() sequencing, so I pushed it into the Update() to move the player during the first frame…nothing has made any difference. And obviously, I’ve searched through all the code for any other place that might be changing the player’s location, but there is no where else except controls based movement.
So…I’m kind of at a loss.
Any suggestions on where to try to troubleshoot from here?
Thanks for any suggestions!