- Home /
yield WaitForSeconds(1) ends with this error:ArgumentException: method return type is incompatible
I just started with the UI in Unity and now i created a working menu with some Buttons. Now i wanted to add a delay with WaitForSeconds(1); in my JavaScript so that if i click one of the buttons it takes a scecond until they load the new Scene. How i made my buttons: I created a canvas and added the Buttons with GameObject-->UI-->button then i created this JavaScript:
#pragma strict
function Start () {
}
function StartGame() {
Application.LoadLevel(1);
}
function Beenden(){
Application.Quit();
}
function Anleitung() {
Application.LoadLevel(2);
}
function Optionen() {
Application.LoadLevel(3);
}
I added this script to the Main Camera and added the Main Camera to the Buttons: ![alt text][1] For every button i choosed one of the functions. My problem is when i add the command WaitForSeconds(1); to one of the funtions and click the button with this function i get this Error:
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..ctor (System.Object target, System.Reflection.MethodInfo theFunction) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:99)
UnityEngine.Events.PersistentCall.GetRuntimeCall (UnityEngine.Events.UnityEventBase theEvent) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:355)
UnityEngine.Events.PersistentCallGroup.Initialize (UnityEngine.Events.InvokableCallList invokableList, UnityEngine.Events.UnityEventBase unityEventBase) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:518)
UnityEngine.Events.UnityEventBase.RebuildPersistentCallsIfNeeded () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:679)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:715)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()
Whats the problem can someone please explain how to fix this in a easy way because im very new to Unity and Java.
Answer by _joe_ · Mar 28, 2015 at 05:28 PM
I advise you to use Invoke in this case, which is simpler and gets the job done:
Invoke("MyFunction", 2); //2 is the number of seconds
function MyFunction(){ Application.LoadLevel(1); }
Of course it is advisable to use Coroutines (and to switch to C# in that case) for later, more advanced control.
Thanks that worked I had to change your command a Little bit because the command automatically started after 2 seconds but now everything works fine. Oh and in don't use C# because making Games is more like a Hobby for me mostly I only search for the things I need in the Internet try to find out how they work and then use it. Surely C# isn't that more harder to learn maybe its easier to learn but I think Java is ok for the Little games I make.
Answer by maccabbe · Mar 28, 2015 at 05:08 PM
WaitForSeconds can only be used with a yield statement in coroutines.
http://docs.unity3d.com/ScriptReference/WaitForSeconds.html
Based on what you described, it is time for you to learn about coroutines
https://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines
Using WaitForSeconds in Javascript automatically changes the function into a Coroutine
Oh I mean I used the command yield WaitForSeconds I only forgot it in my Problem description but in my Script which gave me the error I used yield WaitForSeconds(1); if you mean that with "WaitForSeconds can only be used with a yield statement in coroutines." haven't read/watched the tutorials yet.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191504im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I make a new line in Unity UI text? 2 Answers
UnityEngine.UI.Graphic.OnRebuildRequested () 2 Answers
Canvas Problem 1 Answer
Failed to re-package resources even I make blank project 3 Answers
UNITY NETWORK SCOREBOARD 0 Answers