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 Charva1976 · May 01, 2013 at 12:35 AM · c#pausecoroutinesienumerator

How do I pause a script while until after a function is done running?

Okay, so in UnityScript this : function Start() {

     StartCombat();
 
     var combatCameraController : CombatCameraController = combatCamera.GetComponent(CombatCameraController);
     combatCameraController.MoveFromToPosition(transform.position, transform.TransformPoint(4,6,0), 2.0);
     combatCameraController.RotateToObject(transform, 2.0);
     yield WaitForSeconds(2.0);
 }

Would pause the Start() for 2 secs while the camera moves into position, then continue with the rest of the combat script.

How the heck do i achieve that in C#. I've been playing with StartCoroutines and it just doesn't work the same. I just want to pause the script to let the camera move, then resume. the C# moves the camera, but doesn't pause the script. Please help. void Start () {

     StartCombat();
     CombatCameraController combatCameraController = combatCamera.GetComponent<CombatCameraController>();//move combat camera
     StartCoroutine(combatCameraController.MoveFromToPosition(transform.position, transform.TransformPoint(4,6,0), 2.0f));
     StartCoroutine(combatCameraController.RotateToObject(transform, 2.0f));        
 //    yield return new WaitForSeconds(2.0f); no workie    
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

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by s_guy · May 01, 2013 at 12:55 AM

See StartCoroutine for some good examples. Just click the Javascript to C# button on that page.

You'll see that some of the c# differences are:

  • Coroutines require IEnumerator as a return type

  • you'll need to use "new" with WaitForSeconds()

  • use "yield return StartCoroutine()" for a blocking coroutine. This is the 2nd example in the link above and I think is what you want.

  • use "yield return null" for a simple yield of one frame

See also Coroutines and Yield

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 Charva1976 · May 01, 2013 at 04:45 PM 0
Share

For some reason beyond me, When I made the start() into a IEnumerator, start wasn't running. So I created a new script and copied the entire code bit by bit into a new script, that new script works fine. I was pretty sure I was following the examples from the documentation correctly. Ah well.

avatar image
1

Answer by Loius · May 01, 2013 at 12:45 AM

Unityscript lets you be a lot more lazy than it should. In C# you must specify IEnumerator as the return type of all coroutines:

 public IEnumerator Start() {
   // now yield works
 }
Comment
Add comment · 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
0

Answer by trs9556 · May 01, 2013 at 01:10 AM

If you didn't want to use yield inside another coroutine you can do what I usually do.

You can create two variables, one that lets you know if you should be moving the camera, and one to know if you should continue with the script.

So in your update you have

 if(movingCamera){
     //code to move camera
     if (Mathf.Abs((Camera.transform.position.x - TheDesiredLocation.position.x)) < 0.05) {
         doRestOfCode = true;
         movingCamera = false;
     }
 }
 if(doingRestOfCode){
     //code to do rest of code
 }

And when you want to move your camera make doingRestOfCode equal false and movingCamera equal true. This idea is most effective when you're using slerp/lerp to move your object.

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

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

13 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

Related Questions

Run Coroutine once, but finish Lerping 2 Answers

How to ''stack'' coroutines, and call each one till all are executed? 5 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Call Coroutine from value change event. 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