Yield until color has been chosen, return color (or Color index)

Hey guys,

I need your help with a little “problem”.

I want to choose a color by calling a method in a ColorPicker class. A color picker then opens and when the user has chosen the color, the method is supposed to return the chosen color or in another method the color index.

I’m almost there, but I don’t understand how to implement the yield return here …

Here is what I’ve got.

I want to call the method like this:

int newColorIndex = ColorPicker.chooseColorIndex();

Here is the method:

public int chooseColorIndex() {

		colorChosen = false;

		openColorPicker ();

		while (!colorChosen) yield return null;

		return chosenColor.colorIndex;

	}

This is what happens when a color button is pressed:

public void assignColor() {

		chosenColor = UIEventTrigger.current.GetComponent<ColorPickerColor> ();

		colorChosen = true;

		closeColorPicker ();

	}

Now I want chooseColorIndex to return the index to the variable it has been called from (newColorIndex). I guess I’m messing up the yields here … I am kind of new to coroutines and yields, so could you please help me out?
Edit: I know now that i need the return type IEnumerator and a Coroutine, so please consider the code above as pseudo-code to illustrate what I am trying to do.

How do I tell chooseColorIndex to wait until the user has chosen the color, then return the color index (which is read from a component in the button)?

Thanks!

ps.: here’s the error I get

The body of `ColorPicker.chooseColorIndex()' cannot be an iterator block because `int' is not an iterator interface type
  1. Coroutines must return IEnumerator, that’s why compiler complains about int.

  2. To call coroutine, you have to use StartCoroutine, e.g. StartCoroutine(ColorPicker.chooseColorIndex())

  3. You can’t call coroutine and wait for it to return a specific value. That’s not the way coroutines work. Instead, you can declare a variable in your class, and set it to a specific value inside coroutine.

You can change your chooseColorIndex method to:

public IEnumerator chooseColorIndex()
{
    colorChosen = false;
    openColorPicker ();
    while (!colorChosen) yield return null;

    // do what you want with color index here
}

and call it with:

StartCoroutine(ColorPicker.chooseColorIndex());

If you want to read more about coroutines, I strongly suggest [this UnityGems article][1].

Alternatively, you can avoid coroutine at all - just call ColorPicker.chooseColorIndex(), and then check ColorPicker.colorChosen in Update method. If it becomes true, execute the code you need. This requires both colorChosen and chosenColor to be public/internal static.

EDIT: another option, maybe better in your case - pass some action to coroutine. E.g.

public static IEnumerator chooseColorIndex(System.Action<int> action)
{
    colorChosen = false;
    openColorPicker ();
    while (!colorChosen) yield return null;

    action(chosenColor.colorIndex);
}

and call it with:

System.Action<int> action = (i) =>
{
    sprite.color = availableColors*;*

};
StartCoroutine(ColorPicker.chooseColorIndex(action));
[1]: unitygems.com - unitygems Resources and Information.