Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Khena_B · Feb 20, 2017 at 08:30 PM · coroutinefunctionreferencestaticpublic

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · Feb 20, 2017 at 08:56 PM 0
Share

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.

avatar image Khena_B RobAnthem · Feb 20, 2017 at 09:05 PM 0
Share

Thanks, i'll keep this in $$anonymous$$d

avatar image RobAnthem Khena_B · Feb 20, 2017 at 09:10 PM 1
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Khena_B · Feb 20, 2017 at 08:41 PM 1
Share

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.

avatar image MarshallN Khena_B · Feb 20, 2017 at 08:43 PM 1
Share

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!

avatar image Khena_B MarshallN · Feb 20, 2017 at 08:47 PM 0
Share

That's really interesting, i'll give it a try, thanks!

avatar image
1

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Khena_B · Feb 20, 2017 at 08:39 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutine cannot be automatically started from a static function 1 Answer

Get a public variable to the function. 3 Answers

How to use a float value from coroutine 1 and use in coroutine 2? 1 Answer

C# Global Dynamic Arrays 2 Answers

AI function/coroutine animation issues 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges