Possible bug in Unity3D?
Apparently after the update of Unity3D I can no longer switch from one scene to another. I do not know if it's a problem due to the update but at the moment I get this error when I click on a button that should send me back to another scene.
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/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/build/mcs/class/corlib/System/Delegate.cs:276)
UnityEngineInternal.NetFxCoreExtensions.CreateDelegate (System.Reflection.MethodInfo self, System.Type delegateType, System.Object target) (at /Users/builduser/buildslave/unity/build/Runtime/Export/WinRT/NetFxCoreExtensions.cs:11)
UnityEngine.Events.InvokableCall..ctor (System.Object target, System.Reflection.MethodInfo theFunction) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:149)
UnityEngine.Events.PersistentCall.GetRuntimeCall (UnityEngine.Events.UnityEventBase theEvent) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:447)
UnityEngine.Events.PersistentCallGroup.Initialize (UnityEngine.Events.InvokableCallList invokableList, UnityEngine.Events.UnityEventBase unityEventBase) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:610)
UnityEngine.Events.UnityEventBase.RebuildPersistentCallsIfNeeded () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:776)
UnityEngine.Events.UnityEventBase.PrepareInvoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:812)
UnityEngine.Events.UnityEvent.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
Currently the error happens with the 2018.1.0f2 version and unfortunately I can not remember the version I used previously.
Here is the code relative to the Script responsible of the scene switch, and this works fine:
using UnityEngine.SceneManagement;
using UnityEngine;
public class MainEngine : MonoBehaviour {
// Use this for initialization
void Start () {
DontDestroyOnLoad(this.gameObject);
SceneManager.activeSceneChanged += ChangedActiveScene;
SceneManager.LoadScene("Introduction");
}
// Update is called once per frame
void Update () {
}
private void ChangedActiveScene(Scene current, Scene next)
{
string currentName = current.name;
if (currentName == null)
{
// Scene1 has been removed
currentName = "Replaced";
}
Debug.Log("Scenes: " + currentName + ", " + next.name);
}
}
But this following is the "guilty" code:
using UnityEngine.SceneManagement;
using UnityEngine;
using System.Collections;
public class InstructionsScene : MonoBehaviour {
IEnumerator PlayNow()
{
Debug.Log("scene should be changed");
float fadeTime = GameObject.Find("SceneEngine").GetComponent<Fading>().BeginFade(1);
yield return new WaitForSeconds(fadeTime);
SceneManager.LoadScene("Instructions", LoadSceneMode.Single);
}
}
PlayNow
1 Answer
1
A callback for a button (possibly UI elements in general) has to be of return type void
. Yours is of return type IEnumerator
(if PlayNow
is the function you call from your button).
return type void
return type IEnumerator
PlayNow
A workaround is to start the coroutine from within PlayNow
:
PlayNow
public void PlayNow()
{
Debug.Log("scene should be changed");
StartCoroutine(PlayNowCoroutine());
}
IEnumerator PlayNowCoroutine()
{
float fadeTime = GameObject.Find("SceneEngine").GetComponent<Fading>().BeginFade(1);
yield return new WaitForSeconds(fadeTime);
SceneManager.LoadScene("Instructions", LoadSceneMode.Single);
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Question updated. 👍
– kitsune
5 hours ago