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

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

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
}

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.