- Home /
ArgumentException: method return type is incompatible
I'm sending an integer via a UI button event in 4.6 to this (simplified) function and it's giving me a runtime error. Any ideas what I'm doing wrong?
public IEnumerator ChangeScreen ( int newScreen )
{
yield return null;
}
Full error looks like this:
ArgumentException: method return type is incompatible System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Delegate.cs:190) System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Delegate.cs:276) UnityEngine.Events.InvokableCall`1[System.Int32]..ctor (System.Object target, System.Reflection.MethodInfo theFunction) UnityEngine.Events.CachedInvokableCall`1[System.Int32]..ctor (UnityEngine.Object target, System.Reflection.MethodInfo theFunction, Int32 argument) UnityEngine.Events.PersistentCall.GetRuntimeCall (UnityEngine.Events.UnityEventBase theEvent) UnityEngine.Events.PersistentCallGroup.Initialize (UnityEngine.Events.InvokableCallList invokableList, UnityEngine.Events.UnityEventBase unityEventBase) UnityEngine.Events.UnityEventBase.RebuildPersistentCallsIfNeeded () UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) UnityEngine.Events.UnityEvent.Invoke () UnityEngine.UI.Button.Press () (at /Applications/buildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Applications/buildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Applications/buildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at /Applications/buildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()
Answer by Essential · Nov 27, 2014 at 07:31 PM
I think it is because I have set the function to be IEnumerator and the event system can only call non-returnable functions.
It's a bit of a bug. An IEnumerator method won't show up as a possible choice in the event properties, but if you begin it as void, select it in the event list, then change its type in the script later, it'll still be chosen in the event properties and that's when you start getting errors.
I am having a similar issue, I am using a static Co-routine Utility function in a seperate C# script as an alternative to "yield WaitForSeconds()" in a javascript for a UI controller. I Am doing this as the standard timescale is 0 and need to work around that, but I am having no luck either.
Yes its Best answer , I think we could do it by a parallel function with event function , that activated by simple Boolean and do all of operation in that function ins$$anonymous$$d of event function
Answer by mikolo · Jan 30, 2015 at 03:37 PM
i have a solution of the yield Problem. Unity Version 4.6.1
public IEnumerator ChangeScreen ( int newScreen ) { test();
}
function test() { yield return null; }
What worked for me was
public void NextLevel(String name){
//do something
float somethingTakes = 2f;
WaitForSecondsDo(somethingTakes);// 2f = 2 seconds.
Scene$$anonymous$$anager.LoadScene(name);
}
IEnumerator WaitForSecondsDo(float secToWait){
yield return new WaitForSeconds(secToWait);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613172603im_/https://answers.unity.com/themes/thub/images/avi.jpg)