I am using OnGUI function and creating Buttons in scene. And one button triggers open old scene with this code:
SceneManager.LoadScene (3);
But when the new scene opens, the buttons is still shown. How can i prevent it ?
I am using OnGUI function and creating Buttons in scene. And one button triggers open old scene with this code:
SceneManager.LoadScene (3);
But when the new scene opens, the buttons is still shown. How can i prevent it ?
I am using OnGUI function and creating Buttons in scene.
I think that sums up your problem. I assume the script below is similar to how you are creating the Buttons.
public Texture btnTexture;
void OnGUI()
{
if (!btnTexture)
{
Debug.LogError("Please assign a texture on the inspector")
return;
}
if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
Debug.Log("Clicked the button with an image");
if (GUI.Button(new Rect(10, 70, 50, 30), "Click"))
Debug.Log("Clicked the button with text");
}
The OnGUI
function will always create Buttons or UI controls inside its function every frame. It does not get destroyed when new scene is loaded as long as it is attached to a GameObject.
When your new scene is loaded, check if that script is attached to a GameObject. I assume it is. If it is, it will always execute the code inside it.
Do not use the OnGUI
function in Unity. Start using Unity's new UI System. Here is a Unity official tutorial to get you started on that. You can check this post for how to detect clicks on the new UI system from code.