- Home /
Static and Coroutines help
Hey,
I've made an AudioManager class that handles all my audio, it has a bunch of public static functions that can be accessed anywhere without reference. However i've been trying to do a fade out which requires the use of a coroutine to gradually lower the volume, I can't make a coroutine static or start a coroutine from a static function, then what can i do?
I know i can find my audio manager and get a reference to it's AudioManager script and then start the coroutine using that, but it would be nice to be able to start it without having to make a reference.
Is this possible?
It is actually poor program$$anonymous$$g to write static functions or members in a non-static file. You need to learn more about instancing and obtaining references... Actually a lot of new Devs make this mistake. IT WILL BECO$$anonymous$$E a horrible habit if you keep doing it. Just my two cents. Best practice is to have a single static file called Game$$anonymous$$anager or something, that obtains and holds references to instances. However for 2 monobehaviours to communicate, it is even easier.
If you need any suggestions for avoiding this, feel free to ask. I know it may seem like I'm being ridiculous, but in program$$anonymous$$g it is not simply good enough for your code to "work as intended", how you get to that point is almost more important. It's all about the journey heh.
Answer by HenryStrattonFW · Feb 20, 2017 at 08:36 PM
You can't really do this without accessing the instance of the AudioManager as a coroutine needs to run on a monobehaviour (well technically you can manually process them yourself but you still need a frequently called update method).
Based on you use case of an AudioManager you may want to consider making it a Singleton, which is often a controversial pattern to use in games, but as long as they are not overused, they are perfectly reasonable. This means you are still referencing the instance of the AudioManager in the scene, but accessing it is simpler. ie instead of doing a Find request based on the type, you can simply use AudioManager.Instance.DoStuff() and then your methods don't have to be static and as such can start coroutines with no problem.
Hope this makes sense , and is of some help.
It makes sense, i've been reading about Singletons vs Static etc, i haven't tried the Singleton so i guess now's the right time, that or i'll just make a reference.
Actually, just check out the answer on this question: http://answers.unity3d.com/questions/1093761/coroutine-cannot-be-automatically-started-from-a-s.html
Seems to be just what you're looking for!
That's really interesting, i'll give it a try, thanks!
Answer by MarshallN · Feb 20, 2017 at 08:33 PM
You could have a public static bool that the static function sets to true, and then in Update() have code that checks to see if the bool is true, and if so starts the coroutine and sets the bool to false?
This is what i was about to try but it seemed a bit messy so i asked here, i think i'd rather make the reference, hopefully having a lot of "FindGameObjectWithTag" in Start() isn't an issue.