- Home /
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
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
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.
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
}
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.
Your answer
Follow this Question
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