Kind of vague questions that I am tempted to try to answer by trial and error but afraid I might get away with breaking “da rules” in small tests that don’t work in larger projects because of more active GC or whatever.
Some background…
I am new to Unity but not to C#. I have been using it professionally for about 15 years. I consider myself a pretty advanced user. I am working with audio. I kind of came in through a side door. I am looking to develop my hobby of music composition into a side gig and then maybe my only gig for semi-retirement in two or three years. Anyway, I pretty quickly discovered what you probably know - there are a ton of people trying to do music for games and it is difficult to get started. I looked into the engines and found that Unity was a very friendly environment for me, so I learned a bit about the audio objects (probably not enough yet) and offered to collaborate with independent game devs to add the music and/or sound effects into games. That worked and I am now doing some development. It’s going well, but I am getting far enough into it to start thinking about a good general approach that isn’t snippets here and there with AudioSource hung off objects all over the place.
OK, actual questions…
I know that scripts are classes and that every time you attach one Unity creates an instance. But I wonder about class statics. Are class statics completely global? So that a value set at any point on any scene will retain the value until changed or the game ends?
What happens if you set a static member variable to an object in one scene when you switch to another scene?
Is it possible to create global instances of AudioSource and AudioClip objects?
Can you dynamically add scripts (creating an instance) to objects at run time?
And finally, is there a GameObject that is always “live” through the whole game that I can add components to?
I thought about breaking this up into multiple questions but they are kind of interrelated. I am doing some dynamic loading and switching of AudioClips and making sure I have AudioSources where I need them and finding I am doing the same things - applying music or in game sound volume to AudioSources for example - in lots of different places.
Thanks!