Game shows blank screen when restarting

So I created a game on Android using Unity 5.4.0f3 and wrote a code to pause it using the Escape button and to close it by pressing the Escape button while paused.

void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (!GlobalVariables.paused) // if game is not yet paused
            {
                GameObject.Find("MainCamera").GetComponent<GlobalVariables>().SendMessage("Pause");
                GameObject.Find("PostHUD").GetComponent<PostHUDHandler>().SendMessage("Results", ScoreController.coinCount);
               
//we pause it by sending some messages to classes

            }
            else // if game is paused and the Escape button is pressed, it's the second press. QUIT
            {
                #if UNITY_EDITOR
                UnityEditor.EditorApplication.isPlaying = false;
                #endif

               
                Application.Quit();
            }
        }

    }

This allows me to close the game but when I try to re-open it, I get nothing but a white screen and it’s impossible to do anything, forcing me to force stop the app.

It’s also worth noticing that this happens to me only if I quit during my second, third or third game (in other words, if I quit after losing once, it never happens if I quit during my first game).

Any advice?
Thanks in advance

I can give some advice:
Try to avoid using Find, SendMessage and GetComponent. The first 2 are entirely avoidable, the third should be used sparingly. It won’t fix your issue but it’s good practice. Also, you check GlobalVariables.paused, and 2 lines later you get it as a component from MainCamera. I would guess GlobalVariables is a singleton or static class, then I think accessing it through the camera’s getComponent is redundent.

Try to get a reference to your objects on Awake or Start, if you must. for example:
void Awake
{
myCamera = Camera.main;
my postHud = PostHudManager.Instance.GetComponent();
}

As For your issue, I unfortunately don’t know the answer, but I can tell you I have similar issues on android. once in a while, my display will be all black when I get past the splash, and I need to restart the phone to fix that (tested on note5). I suspect that’s some kind of openGl corruption, but I can’t figure out what exactly is wrong.

1 Like

Yeah I can understand why, I seem to be the only one to have this problem… thanks anyway
but while we’re on the subject, what are some good alternatives to Find and SendMessage when I need to call a method from another class?

The elegant way is to keep refetences to your objects, or when it is not possible, I like to use a manager/singleton pattern to give access to objects.

Find basically uses the scene as a list of your objects, and you browse that list for a particular object name. It’s convenient but terrible for performance.

For example, if you have a hud, making it a singleton makes sense as you probably dont have more than one in a scene. Then a bit like for camera where you can use Camera.main to access the main camera without a find, you could use Hud.myInstance to access your hud without a find. You can also add members to your class. Hud needs to access a button? Add a button attribute to Hud, and assign it in the inspector, or when you create the Hud.

As for sendMessage, you can call any public function directly on a component. For example
GameObject myEnemy;
myEnemy.getComponent().AnyFunction();

1 Like

Got it, OK thanks man :slight_smile: