Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException when re-loading scene and re-activating wit #77

Open
phillipsalbright opened this issue Jul 6, 2022 · 2 comments

Comments

@phillipsalbright
Copy link

phillipsalbright commented Jul 6, 2022

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.

@phillipsalbright phillipsalbright changed the title NullReferenceException when re-loading/activating wit NullReferenceException when re-loading scene and activating wit Jul 6, 2022
@phillipsalbright phillipsalbright changed the title NullReferenceException when re-loading scene and activating wit NullReferenceException when re-loading scene and re-activating wit Jul 6, 2022
@SilviaCebrian
Copy link

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.

@Lorenz-Possnig
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants