Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by thewalrusnp · Oct 05, 2013 at 01:33 PM · coroutinestate-machinestates

CoRoutine without MonoBehavior possible?

Hello,

I just started using Unity and I would like to know if there is a way to bypass an issue I'm having with Coroutine in Unity.

I created a singleton inheriting from MonoBehavior that will be shared between scenes. This singleton (StatesManager) contains a scene state member that will be set by the game between each scene. Upon the call on StatesManager::Update() or StatesManager::UpdateGUI() methods of that singleton, the actual set state's UpdateState() and UpdateStateGUI() methods will be called. Once the state is changed by changing of scene, UpdateState() and UpdateStateGUI() from the new state will be called instead by the StatesManager.

My problem is that inside that scene state, I am calling different objects (as an example, a GUI file) which do not inherit from MonoBehavior. How am I supposed to use Coroutine in that case?

An example is that I have a state set in StatesManager, StartMenuState, and that state, just like all others, is not inheriting from MonoBehavior as I want my StatesManager to be the one having Update()/UpdateGUI(). Upon creation of the StartMenuState, I create all objects that are required by the state, such as a StartMenuGUI object. Inside that GUI object, I have code that creates 3 rectangles and handle the case when I click on one of the rectangle, I need to launch a StartCorouting(WaitForRequest()) on a WWW object. However, since StartMenuGUI does not inherit from MonoBehavior, Coroutine won't work... I thought about making it inheriting from MonoBehavior, but I won't be able to call 'new' on the StartMenuGUI class. Any idea on how I should handle this type of situation? What could I do to be able to create my different systems inside my state that requires Coroutine stuff?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by whydoidoit · Oct 05, 2013 at 01:42 PM

Create a dummy object (DontDestroyOnLoad if you like), attach a simple monobehaviour to it (doesn't have to do anything except perhaps register itself as a singleton) and then start the coroutines on that. You have to watch out for potentially null things happening inside the coroutine if objects are being loaded and destroyed between scenes.

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 Owen-Reynolds · Oct 05, 2013 at 03:30 PM 0
Share

For example, I have a non-monobehavior menu class, which sometimes spawns things and want to run coroutines.

For hooks to the spawnable prefabs, I already have a "menuItemHolder" script, on an empty, which is just lots of public Transform buttonPrefab;'s and no Start or Update. If anything in the scene will use the menu class, you drag in one copy of the menuItemHolder object.

So, since it's already required to be in the scene as a monobehavior, and the menu class already "knows about" it to spawn stuff, its the perfect place to add coroutines (which need to be given a reference to the menu that called them, but nothing wrong with that.)

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

17 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

Related Questions

Question regarding FSM, are multiple Machines on the same Object recommended? 0 Answers

Coroutines or update method with switch statement as state machine 2 Answers

Best practice for programmatic character animation 2 Answers

Yield on a Function Typed Variable 0 Answers

StateMachineBehaviour + StartCoroutine() 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