Why does FindObjectWithTag work in editor but not in build?

So I’ve spent almost 1 week trying to solve the fact that my game worked fine in the editor but not in the build. I spent most of my time configuring the execution order and doublechecking that I used Awake and Start methods correctly. Nothing helped as I still got a null reference in one of my scripts. I then changed from finding with tag to simply finding the type, and to my surprise it worked! Now my question is what’s going on? There is obviously something wrong with the tag. restarting Unity does not solve it. The player object is a prefab placed in the scene from start, it is never deactivated during the entire runtime. Even the Tag is the unity default “Player” tag.

Thanks in advance!

It’s possible that children of your Player object also have the Player tag. When multiple objects with the same tag are present FindGameObjectWithTag doesn’t guarantee that it will return a specific GameObject. So it’s perfectly possible that it returns the object you want in editor and another object in build.

Conclusions are that this method (along with Find and any other method that searches by string) is horrible and shouldn’t ever be used.