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
2
Question by roberto_sc · Jul 13, 2011 at 09:04 PM · instantiatecoroutinesave

Instantiate + Coroutine = problem

How could I instantiate (i.e. create a copy with the exact same state) game objects that starts coroutines, since Unity Instantiate won't save the coroutine state? Example:

 public class CoroutineTest : MonoBehaviour {

 public IEnumerator DoIt()
 {
     Debug.Log("First print");
     yield return new WaitForSeconds(15.0f);
     Debug.Log("Last print");
 }

 void Start()
 {
     StartCoroutine(DoIt());
 }

 [ContextMenu("Clone")]
 private void Clone()
 {
     var obj2 = (CoroutineTest)Instantiate(this);
 }

}

If I hit Play and after some 5 secs Clone, I'll have this answer:

 First print
 First print
 Last print
 Last print

While in the perfect world I would have:

 First print
 Last print
 Last print

I'm looking for solutions that won't make me implement a state machine inside the coroutine.

Comment
Add comment · Show 5
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 Waz · Jul 13, 2011 at 09:19 PM 0
Share

Interesting. It's arguably a bug, but not likely to change any time soon, because by the same argument, Start should not be called on clones that have already started, there children should keep their active states, etc.

Sorry, but I think your going to have to be able to recover the Coroutine state yourself from instance variables, which means a state machine in the coroutine.

avatar image Bunny83 · Jul 14, 2011 at 02:25 AM 1
Share

"Start is only called once in the lifetime of the behaviour". A clone of a script (including the GameObject) is a completely new instance and therefore another $$anonymous$$onoBehaviour. When it is created, Awake is called (from the internal constructor) and next frame, Start is called.

Instantiate will not make a deep memory copy (which wouldn't help btw.). It's just serializing the object (public fields and all fields marked with SerializeField) and deserialize the values on the new created object.

Coroutines are actually statemachines. Every coroutine will produce a seperate internal nested class that represents the coroutine. The class just implements the IEnumerater interface and all the code you've writting in your coroutine will go into the $$anonymous$$oveNext() function which has some kind of a switch-case construct in it. All that is hidden from you and Unity. That's completely handled by the C# / $$anonymous$$ono compiler.

Here are some great articles about coroutines in C#:

A custom coroutine scheduler:
http://www.unifycommunity.com/wiki/index.php?title=CoroutineScheduler

A great example what happens behind the scenes when you use "yield"
http://startbigthinksmall.wordpress.com/2008/06/09/behind-the-scenes-of-the-c-yield-keyword/

avatar image Joshua · Jul 14, 2011 at 04:23 AM 0
Share

@Bunny83 this is also a very well written and comprehensive article about coroutines http://altdevblogaday.com/2011/07/07/unity3d-coroutines-in-detail/

avatar image Bunny83 · Jul 14, 2011 at 04:27 AM 0
Share

Yeah, really nice a bit more about the scheduling stuff. We can only guess how Unity implemented their scheduler but i think it's quite similar.

avatar image Joshua · Jul 14, 2011 at 04:32 AM 0
Share

Read the comments, Lucas $$anonymous$$eijer from UT tells him is absolutely spot-on! And he says some other stuff.. then I had to tell him he was wrong. :p

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Objects instantiated in custom editor not saving. 0 Answers

Avoid altering prefab at runtime 1 Answer

How do you make a script that creates a new scene on runtime? 1 Answer

Working projectile scripts do not work due to if condition 1 Answer

Coroutine being iterated in an unrelated function 0 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