Script working in editor But not woking in Build.

In this game I am working on, everything works Completely well in the unity editor. No errors in the console. But when I build the project to test on my android phone only one portion of a script doesn’t work. I tried building this for PC and WEB, the same problem. In the code, in the {If SafeToFall ==true} part, The Destroy() method works. That means the boolean is working. But after that, The player doesn’t Change its Transform.
Instead, only a part of the player transform to SafeSpawn.position.(Player is made of a Sphere and some cubes). I want to be clear. The game works without any problem in the editor. Only the builds have the problem. I even tried transferring the project to a different computer and build there. No change. Every build has this problem. Should I write the whole thing from scratch? :frowning: How to fix this?

 if (SafeToFall == true)
        {
            Debug.Log("log4");

            Destroy(SafeFallIndicator);
            
            if (player.gameObject.transform.position.y < -10)
            {
                Debug.Log("log5");

                player.gameObject.transform.position = SafeSpawn.transform.position;
            }

        }
        else
        {
            if (player.gameObject.transform.position.y < -10)
            {
                Debug.Log("log6");
                SceneManager.LoadScene("Fail" /*, LoadSceneMode.Additive*/);
            }

        }

That’s certainly one approach… not a very useful one. I suggest you learn how to debug!

For this particular case, are you using Rigidbodies? Did the player’s Rigidbody perhaps fall asleep? If so wake it!

The way you interactively figure out what is going on with a game is either by attaching the debugger (useful, but awkward because breakpoints FREEZE your game), or by adding Debug.Log() statements.

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 are the values of the variables involved? Are they initialized?

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

If you are running a mobile device you can also see the console output. Google for how.

1 Like

I Hear You. I should learn how to debug a build. Thanks!

I fixed it. The problem was all the objects the player is made of shared the same tag as the parent of the Player.
In the script, I was using FindWithTag(). Apparently, the android build doesn’t like that even though it worked fine in unity. I removed the Player tag from all the child objects so, only the Parent Object has the Player tag.
After that, the build worked no prob. Lesson Learned. Don’t assign tags unless you are going to use them. Cheers!:slight_smile:

If you haven’t done so already, install Android Logcat from the Package Manager. You can use it to easily check for errors that only happen in the build.

1 Like

Or even https://assetstore.unity.com/packages/tools/integration/log-viewer-12047 is another invaluable tool for debugging on mobile.