- Home /
Unsolvable issue with System.Action and IEnumerator
I have 2 scripts. One in UnityScript called Calling.js, another in C# called DelegateHandler.cs, here are the two scripts:
Calling.js:
function Awake ()
{
var myHandler : DelegateHandler = DelegateHandler.Get().OnEvent( Callback );
myHandler.TryHandler();
}
function Callback( _string : String ) : IEnumerator
{
//yield WaitForSeconds(1);
print("value = " + _string);
}
And DelegateHandler.cs:
public class DelegateHandler : MonoBehaviour
{
public static System.Action<string> callbackAction;
public static DelegateHandler Get()
{
DelegateHandler delegateHandler = GameObject.Find ("Main Camera").AddComponent<DelegateHandler>();
return delegateHandler;
}
public DelegateHandler OnEvent( System.Action<string> _callback)
{
callbackAction = _callback;
return this;
}
public void TryHandler()
{
callbackAction("test") ;
}
}
I need OnEvent to be DelegateHandler typed.
Problem is: If I uncomment the yield WaitForSeconds(1) on the .js , the Callback function is never called.
I've searched for hours for workaround with delegates() , abstract methods and everything but I can't find anything to make this work... I think I have teared enough hair of my head to make myself a new wig!
Action delegate are meant to return void, your coroutine returns IEnumerator. Try a Func ins$$anonymous$$d.
I put this as comment because the whole js/c# I am not sure about.
Thanks for this answer. I took few $$anonymous$$utes to understand how Func works. I've replaced
OnEvent( System.Action < string > _callback)
BYOnEvent( Func < string, IEnumerator > _callback)
AND System.Action < string > callbackAction;
BY Func < string, IEnumerator > callbackAction;
But sadly, same issue. Works without the yield. Never called with the yield uncommented
Edit: I put spaces between Func and < string ,etc otherwise it was deleted by the comment ( ? )
Well... I had to wrap callbackAction("test") into StartCoroutine(); It works... like... I mean... finally... thank you so much. Please post an answer so I can accept It
Your answer
Follow this Question
Related Questions
IEnumerator only called once 3 Answers
When to use 'delegate', 'event' or 'Action' ? 1 Answer
Clear event System.Action list 1 Answer
Multiple Actions for a single button 0 Answers
public static Callback problems 0 Answers