How to pass by reference to coroutine in unity

first – it can’t be done

second there is different solution that doing the job, it’s cal callback, we can pass callback to the coroutine, example :

class A


User user;

StartCoroutine(networkConnection.RequestLogin(userFromDb => this.user =              userFromDb));


class networkConnection


public IEnumerator RequestLogin(System.Action userFromDb)


//fetch data from DB and put in local variable “user “, and then …




the user object in class A will be fill when the coroutine finish.

i can also can put logic like this :

suppose user object in class A has variable bool isHelp, so when i call the coroutine it look like this

StartCoroutine(networkConnection.RequestLogin(userFromDb => {



print(“help thanks.”);


} ));

another thing this action help me is when i want to know when the coroutine finish to do something else.

public void LoginButtonTapped()
   log.text = "Logging in...";
   StartCoroutine(networkConnection.RequestLogin(log, userName, password, userFromDb => {
   this.user = userFromDb;
   //the score is ui object where i want to present my score
   score.text = user.score.ToString();

This entry was posted in כללי and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s