wit-ai / wit-unity

Wit-Unity is a Unity C# wrapper around the the Wit.ai rest APIs and is a core component of Voice SDK.

Home Page:https://developer.oculus.com/documentation/unity/voice-sdk-overview/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NullReferenceException when re-loading scene and re-activating wit

phillipsalbright opened this issue · comments

I have been working on a project where I am using wit.ai for voice recegnition in a scene that can be loaded, exited, and reloaded. I noticed that on the first time the scene was loaded (whether it be as the first scene or through SceneManager.LoadScene), my wit.ai connection works fine with my Wit script being the base of the functionality, being activated and used to recognize voice. I noticed that if I exit the scene and reload it, every time I get a nullReferenceException in WitService on line 155, stemming from Wit.cs line 91.

NullReferenceException: Object reference not set to an instance of an object
Facebook.WitAi.WitService.OnEnable () (at Library/PackageCache/com.facebook.witai@07b625a78e/Scripts/Runtime/WitService.cs:155)
UnityEngine.GameObject:AddComponent()
Facebook.WitAi.Wit:Awake() (at Library/PackageCache/com.facebook.witai@07b625a78e/Scripts/Runtime/Wit.cs:91)

Past this point, every time that Wit.Activate() is called in order to obtain voice input, this occurs:

NullReferenceException: Object reference not set to an instance of an object
Facebook.WitAi.WitService.StopRecording () (at Library/PackageCache/com.facebook.witai@07b625a78e/Scripts/Runtime/WitService.cs:297)
Facebook.WitAi.WitService.Activate (Facebook.WitAi.Configuration.WitRequestOptions requestOptions) (at Library/PackageCache/com.facebook.witai@07b625a78e/Scripts/Runtime/WitService.cs:193)
Facebook.WitAi.WitService.Activate () (at Library/PackageCache/com.facebook.witai@07b625a78e/Scripts/Runtime/WitService.cs:180)
Facebook.WitAi.Wit.Activate () (at Library/PackageCache/com.facebook.witai@07b625a78e/Scripts/Runtime/Wit.cs:44)
UnityEngine.Events.InvokableCall.Invoke () (at :0)
UnityEngine.Events.UnityEvent.Invoke () (at :0)
UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

Everything I try to work around this exception ends up landing me with a similar nullReferenceException, always having something to do with the AudioBuffer. Is there something I am missing in terms of leaving and entering scenes with Wit.cs?

Here are steps to reproduce this:

  1. Open an empty unity3d project.
  2. Import this SDK via github url.
  3. Make two copies of the "ResponseDebugger" sample scene in your assets.
  4. Add each of these to the Scenes in build via build settings.
  5. Add a button to each scene tied to a script which loads the other duplicate scene. (just use UnityEngine.SceneManagement and SceneManager.LoadScene(int)).
  6. Generate the wit configuration asset using the server access token for any app, then attach that to the "Wit" prefab in the ResponseDebugger scenes.
  7. Press "play" with one of the scenes open and click "activate mic", validating that Wit is working normally with voice input by retrieving feedback. Make sure the "activate mic" button is tied to Wit.Activate().
  8. Click the placed button to go to the other scene.
  9. Note the NullReferenceException that occurs due to Wit activation.
  10. Note that if you click "Activate Mic", the same exceptions keeps occuring.
  11. You can load back to the first scene and note that despite it working initally, the same error is now being passed.

This problem is really slowing down the progress of my projects and I hope a fix exists.

Hello, do you still have the same problem? I found the solution. When you change scene, the AudioBuffer is destroyed(the object) and not created. You must add DontDestroyOnLoad(this.gameObject) when it is created or have it created again when a scene is loaded.

Hi, I just had the same problem and after looking at the source I figured out two solutions:

  1. Use DontDestroyOnLoad as mentioned by @SilviaCebrian
  2. Create a OnDestroy() method in Facebook.WitAi.Data.AudioBuffer and set _instanceInit to false there.

With 2.) the Object would just exist in the szene it's used in and it would still be impossible to have two AudioBuffers at the same time.