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
0
Question by ronronmx · Nov 18, 2012 at 07:57 PM · coroutinemanagerawake

Using Update() or Coroutine for this problem?

Let me explain, I have a GameStates manager which acts as an FSM to control each state of the game. When the FSM starts, it first runs the "Initialize" state in which I call a function that does some initialization and sends messages to other Managers to tell them it's time to cache some references to other things.

If the initialization was successful, GameStates then sets a static boolean to true: intiDone = true, and switches to the next state, Playing.

Now my other classes rely on initDone to set themselves up. The problem is that sometimes, initDone only becomes true after Update() in those classes, and Update() uses variables that get set when initDone becomes true.

So 1 solution is to use: if( initDone == false ) return; - in Update(), but it seems like a waste of time because it's always gonna have to check it before continuing.

The other solution i thought about is instead of using Update(), I would use a coroutine, that way when initDone = true, I can send a Message to all classes that are waiting for it, and start the coroutine in each class. The coroutine would run every frame just like Update().

But, I couldn't do that for FixedUpdate(), since physics should be in FixedUpdate I cannot have a coroutine running my physics calculations.

I am running into this problem a lot because of the OnEnable() bug which force the class it belongs to to execute before any other class which doesn't have OnEnable() defined. The whole idea behind my GameStates class was to control these kind of things, and it worked great until I started using LoadLevelAdditive() to load things like HUD, Controls and Player into each scene, instead of having to manually place them into each new scene I create.

Since those classes are being loaded after the current's scene classes, it broke my GameStates logic and I am now trying to make it work with classes that come in after the initialization.

Sorry for the long post, and thanks in advance for your time! If anyone could maybe give me some advice I would really appreciate it :)

Thanks guys, Stephane

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

2 Replies

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

Answer by whydoidoit · Nov 18, 2012 at 10:44 PM

So you can do Physics in a coroutine if you use

   yield return new WaitForFixedUpdate();

You could also use a delegate in your Update function and switch it to point to something else when you are ready - though that's probably slower than an if check - neither are going to cost you much. The delegate method is good for state machines though - because you can switch between multiple - see my FSM article on Unity Gems

   Action updateFunction;

   void Start()
   {
       updateFunction = BeforeInitUpdate;
   }

   void BeforeInitUpdate()
   {
       //Any before init update logic
       if(initDone)
           updateFunction = AfterInitUpdate;
   }

   void AfterInitUpdate()
   {
       //Normal update functions
   }


   void Update()
   {
        updateFunction();
   }
Comment
Add comment · Show 9 · 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 ronronmx · Nov 18, 2012 at 11:08 PM 0
Share

Oh man I didn't know about yield return new WaitForFixedUpdate(); hahaha thanks for pointing that out!

I'm reading your FS$$anonymous$$ article on Unity Gems right now...I have a lot to learn about proper uses of FS$$anonymous$$, your example above makes a lot of sense and I'm kinda mad at myself for not trying to learn more about this type of stuff. You get to a point when you think you have enough knowledge to actually "Start" writing code you can use in the final version of your game - after all, at one point or another you have to stop "learning" and start "making" right? - but judging from your small code snippet above, I could have used a much better implementation then what I'm doing...

And please post this as an answer so that I can vote and approve it :)

Thanks a lot, I'm gonna try to implement this and I might have a question or 2 later if I'm struggling, if you don't $$anonymous$$d :)

avatar image whydoidoit · Nov 18, 2012 at 11:13 PM 0
Share

Yes you have to start writing code. You have to also finish too :) But there is nothing wrong with refactoring - just not if it breaks everything.

Finish anything, then learn, then do it better next time. That's what I do!

avatar image whydoidoit · Nov 18, 2012 at 11:16 PM 0
Share

That Unity Gems Coroutines++ article might be useful reading/watching too. It's got a nice chart of where they run at least...

avatar image ronronmx · Nov 19, 2012 at 05:25 AM 1
Share

Well, I have been reading articles on Unity Gems for the past 4 hours, none of them which were on FS$$anonymous$$s hahaha...there is so much really good information there, answers to questions that I have had for a while and couldn't get any clear answers on.

I need to actually get some work done tonight, so i stopped reading halfway through the FS$$anonymous$$#2 article - and also because I have a headache...too much information intake... - but I'll be back tomorrow and the day after and after and after etc...

I just wanted to say that the way you explain things is just great, very easy to read and understand, and that's very hard to do - and come by - so I am really grateful :)

avatar image whydoidoit · Nov 19, 2012 at 05:31 PM 1
Share

You should yield before you do physics.

   IEnumerator SomeCoroutine()
   {
        //Not physics


        while(true)
        {
               yield return new WaitForFixedUpdate();
               //Physics
        }
   }
Show more comments
avatar image
-2

Answer by Steve Steven · Nov 18, 2012 at 10:35 PM

Sorry, I didn't read this at full because is too long, I suggest you to avoid the use of the bool initDone, because Unity calls Awake and Start voids at start, after these two it will call Update, which will called every frames. So Update will always be later then Start or Awake. I don't know if this was what you was asking for.

Comment
Add comment · Show 2 · 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 ronronmx · Nov 19, 2012 at 05:19 AM 1
Share

Please Steve, I appreciate you trying to help out, but I have read some of your other answers to other questions, and most of them are wrong or not related to the actual question, which is probably because, like you yourself said, didn't read the whole question...

I suggest that you don't post answers to questions you do not understand or haven't fully read. Ins$$anonymous$$d you should use this site to ask questions yourself and gain knowledge, as there are many great users here with lots of experience to share.

I hope I did not offend you as it wasn't my intention. But you already have a bunch of thumbs down, and that's not a good start if you wish people here to help you out when you have a question.

avatar image Steve Steven · Nov 19, 2012 at 01:43 PM 0
Share

No you didn't offend me, I didn't read the full post, because I had to go to sleep, I am sorry about this.

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

11 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

loading elements on startup 2 Answers

How to access StartCoroutine in a static way 3 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer


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