x


Using Coroutines

I have a function that I want to pause its process, run a second function, and then resume the first function once the second one repeats.

I beleive this is accomplished using coroutines.

Ive never used them before though, could someone explain how? thanks.

more ▼

asked Jun 06, 2011 at 07:38 PM

michael 4 gravatar image

michael 4
357 126 102 103

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
print ("Starting " + Time.time);
// Start function WaitAndPrint as a coroutine
yield WaitAndPrint();
print ("Done " + Time.time);

function WaitAndPrint () {
    // suspend execution for 5 seconds
    yield WaitForSeconds (5);
    print ("WaitAndPrint "+ Time.time);
}

// - prints "Starting 0.0"
// - prints "WaitAndPrint 5.0"
// - prints "Done 5.0"


is this what you are looking for? source: http://unity3d.com/support/documentation/ScriptReference/Coroutine.html

more ▼

answered Jun 06, 2011 at 07:42 PM

iggy gravatar image

iggy
456 2 10

I was trying to use that but I got the error "Cannot implicitly convert type 'void' to 'object'

heres what Im trying

public IEnumerator FreeFly_Camera()

{

if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
    transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
}
if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
    transform.Translate(-Vector3.forward * Time.deltaTime * moveSpeed);
}
if(Input.GetKey(KeyCode.LeftArrow)|| Input.GetKey(KeyCode.A))
{
    transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
}

if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
    transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
}

if(Input.GetKeyDown(KeyCode.Return))
{
    yield return CameraController.Instance.SetNewCameraPosition(); //save this transform as a camera position
}

}

Jun 06, 2011 at 07:46 PM michael 4

what kind of return type is

CameraController.Instance.SetNewCameraPosition()
?
Jun 06, 2011 at 07:55 PM iggy

Looks like he's using javascript- so there's no void.

michael: check out the link iggy posted on coroutines, and set your language to javascript- you'll see some examples of how to use it. I don't think javascript uses anything like the IEnumerator.. can't say for sure though, I use C#.

Jun 06, 2011 at 07:59 PM testure

Im using c# as well

CameraController.Instance.SetNewCameraPosition() doesnt return anything, its just a set of instructions that need to take place before the original function finishes.

Jun 06, 2011 at 08:02 PM michael 4

yes, it returns "void" in that case.

try to set at the end of SetNewCameraPosition().

return true;
Jun 06, 2011 at 08:04 PM iggy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x104

asked: Jun 06, 2011 at 07:38 PM

Seen: 1381 times

Last Updated: Jun 06, 2011 at 08:24 PM