It's not quite clear, but I'll try and cover all bases in case one of them is what you are looking for.
If you want the No button to just hide the dialogue - it's pretty simple... Just change exitbuttonclicked back to false:
// the no button
if(GUI.Button(new Rect(noExitButtonLeft, noExitButtonRight, noExitButtonWidth, noExitButtonHeight), "No")) {
Debug.Log("No Clicked");
exitbuttonclicked = false;
}
When you load a new scene, this script should be destroyed, so you shouldn't have problems with leftover labels or buttons or anything. Is that what you want?
If it's not destroyed (if you used Object.DontDestroyOnLoad), you can destroy it manually using Object.Destroy.
//the yes button
if(GUI.Button(new Rect(yesExitButtonLeft, yesExitButtonRight, yesExitButtonWidth,yesExitButtonHeight), "Yes")) {
// This next line will actually destroy this script, unless it was marked as DontDestroy()
Application.LoadLevel("Main Menu");
}
If you want to keep the script alive, but don't display anything, you can do something like
public bool bDisplayGUI = true;
public void OnGUI()
{
if (!bDisplayGUI)
return;
// Rest of your code goes after this
...
}
Just take into acount that OnGUI() will run potentially several times each frame, and do nothing, but will waste resources while doing it (since obviously it still needs to be called by Unity).
The next option would be to just disable the script...
//the yes button
if(GUI.Button(new Rect(yesExitButtonLeft, yesExitButtonRight, yesExitButtonWidth,yesExitButtonHeight), "Yes"))
{
// This next line will actually destroy this script, unless it was marked as DontDestroy()
Application.LoadLevel("Main Menu");
this.enabled = false;
}
I hope that's what you were after...