Sporadic Issue setting position of GameObject

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!

The code above is hairy. There’s no way to reason about any step of the process.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

Once you have done that, now you can start thinking constructively about what is going on because you have a fighting chance of understanding WHAT is going on.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3