- Home /
Static problems
Hello all,
I was having a problem using DontDestroyOnLoad on my AudioListener where it created a new listener when I went back to the original screen that it originated saying, you should only have one listener. I knew that, but since I'm new, I didn't know that it would create a new object and keep the one I didn't destroy.
So, I took my Background Music script and made it static. All of my errors are gone, but all of my AudioClips don't show up in the inspector and since I'm not giving the variable a reference, nothing will play. I'm sure there's a reason that static fields don't show up in the inspector, but it's really hindering me.
Is there a way to reference an AudioClip that's not attached to a gameobject? Like in my assets folder, but not loaded into the scene? This isn't the only script that I'd like to use this "global" functionality on. I'd like to keep a decent amount of game data for my GUI Overlay too. (I know I can use PlayerPrefs, but in my mind, referencing those every frame seems like it would cost more resources than referencing a global(static) script that holds the same variables.)
I'm not only new to Unity, I'm fairly new to programming in general. Any help or guidance is much appreciated.
You are trying to make a singleton, DontDestroyOnLoad is only half the process. When you load a new scene, you need to check if the instance already exists. See my answer here, also this
Your answer
![](https://koobas.hobune.stream/wayback/20220613141443im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
keep static/global object through scenes 1 Answer
load/have asset on a static script 1 Answer
Static calls to Static GameObject 2 Answers