Hide/Show GUI.Button on iOS

I’ve the following code snippet on iOS (iPhone + iPad):


    bool showPlayButton = true;
	void OnGUI() 
	{
		if (showPlayButton)
		{
			if (GUI.Button(CreatePlayButtonRectangle(), "Play"))
			{
				showPlayButton = false;
				
				// do something
			}
		}
		
	}

Once I tap the Play button, the Play button will disappear. It works as expected.

After finishing the game, I set


   showPlayButton = true;

The Play button shows up again as expected.

However, it does not disappear when I tap it again. I was expecting to hide the button again.

What did I do wrong?

Thanks in advance for your help.

1 Answer

1

I’m sure your problem is that showPlayButton is assigned to true in every update call somewhere in your code. Try to write out some debugging logging every time it happens.

You're correct. I found the bug.. Thanks.

In my tests, GameObjects that are instantiated from script do get a reference to the scene in which they were instantiated, so their scene.name is not null.