- Home /
Question by
lowhearted · Jun 05, 2018 at 05:46 AM ·
c#scripting problemcoroutineienumerator
Can't call of type IEnumerable with delegates?
I am making a controls menu, and this is what I started with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class KeyInput : MonoBehaviour
{
public Button selectButton;
// the button color when it is selected
public int buttonNewRValue;
public int buttonNewGValue;
public int buttonNewBValue;
public int buttonNewAValue;
private void Start()
{
selectButton.onClick.AddListener(delegate { OnSelectButtonClicked(); });
}
private IEnumerator OnSelectButtonClicked()
{
print("STARTED");
// now that we clicked this selection, we indicate so by making the image visable
selectButton.image.color = new Color(buttonNewRValue / 255.0f, buttonNewGValue / 255.0f, buttonNewBValue / 255.0f, buttonNewAValue / 255.0f);
yield return WaitForKey();
KeyCode pressedKey;
foreach (KeyCode code in Enum.GetValues(typeof(KeyCode)))
{
if (Input.GetKeyDown(code))
{
pressedKey = code;
}
}
}
private IEnumerator WaitForKey()
{
while(!Input.anyKeyDown)
{
yield return null;
}
}
}
When the button is clicked, the function OnSelectButtonClicked() is called, but I dont see "STARTED" in the console as the function is actually never called. Anything with type IEnumerable doesn't seem to work here. Any ideas?
Comment
Best Answer
Answer by SohailBukhari · Jun 05, 2018 at 07:46 AM
you did not use the return value of the iterator, so called StartCoroutine(OnSelectButtonClicked()); replace the code in the Start.
private void Start()
{
selectButton.onClick.AddListener(delegate
{
StartCoroutine(OnSelectButtonClicked());
});
}