about globallly accessible script

Recently I came across singleton concept and so I went to try out. Here is what I had made
I created 2 scene, which are same, with a button at middle. At scene 1 , if I press that button, it will go to scene 2. In scene 2,if I press that button, it will go to scene 1. A same audiosource will be played throughout all the scene. The audiosource and the changing scene function will be put into singleton script inside audiosource gameobject at scene 1 , so that when it changes to scene 2, this audiosource will be brought to scene 2, and from scene 2, it will be brought to scene 1 back (since scene 1 already has the game manager, it will be destroyed)

Now the problem come. When I go back to scene 1, I cannot press the button. I check out the “on click” , nothing is attach to it. This happen only when coming back from scene 2 to scene 1. Any idea about this problem? I am really new to unity so please give some help! Thanks! here is the code of singleton script

using UnityEngine;
using UnityEngine.SceneManagement;

public class singleton : MonoBehaviour
{

public static singleton instance;
private void Awake()
{
if (instance != null)
Destroy(gameObject);
else
{
instance = this;
DontDestroyOnLoad(gameObject);
}
}

public void run()
{
SceneManager.LoadScene(1);
}

public void goooo()
{ SceneManager.LoadScene(0); }
}

that is because when you loading scene 1 from 2 the object in scene 1 what is attached to button is destroyed by your script

Destroy(gameObject);

Did you mean that the one got destroyed is not the one moving in from 2 to 1? but the 1 itself?
if that so, why? When gameobject with singleton move from scene 2 to 1, since there is already a instance there, shouldn’t it himself got destroyed?

No. You want to keep the object from scene 2 alive because he most likely contains data that should be transferred between the scenes.

Think about music. You have a singleton GameObject that plays music through multiple scenes. If you destroyed the singleton every time you entered a new scene, the music would start over every single time (That’s because the singleton in the new scene has no idea where the music left off before leaving the new scene), but, if you kept the old singleton and destroyed all new ones, you would have the same music clip play through multiple scenes (That’s because the old singleton knows exactly what state it was in before it left the past scene).

Think about music. You have a singleton GameObject that plays music through multiple scenes. If you destroyed the singleton every time you entered a new scene, the music would start over every single time (That’s because the singleton in the new scene has no idea where the music left off before leaving the previous scene), but, if you kept the old singleton and destroyed all new ones, you would have the same music clip play through multiple scenes (That’s because the old singleton knows exactly what state it was in before it left the previous scene).

Ah I got it! I thought the old singleton when moving to new scene will start the awake function everytime. Just checked it out, it wont, so mean it will keep maintained since it indeed under do not destroy on load. Now everything clear, thanks!

And 1 more thing, when there are 2 singletons,how come the instance in new scene not pointing to null? It detects someone with the same class and thus pointing at it, or what?

Never reference singletons through inspector in your scene. Use them via code only. If you want singleton to handle some button clicks in scene or other events, add intermediate instance script like

public class MainMenuWindow : MonoBehaviour {
  public void HandlePlayClick() => MySingleton.instance.Play();
  public void HandleExitClick() => MySingleton.instance.Exit();
}

Or bind them dynamically using FindObjectsWithTag or some other DI solution which might be more effective, bu requires additional efforts.