Forum rules - please read before posting.

I ran into a curious issue using EventManager.OnInventorySelect

I have a custom component for playing audio from Ui elements. It works great. BUT if I transition from scene to another, the link to the audio source breaks when using EventManager to trigger events.

Audio that is played on update is heard, but when an audio is triggered trough event manager I get the error "ArgumentNullException: Value cannot be null.
Parameter name: source
UnityEngine.AudioSource.set_pitch (System.Single value) (at /Users/bokken/build/output/unity/unity/Modules/Audio/Public/ScriptBindings/Audio.bindings.cs:739)
HotspotHighlighter.itemSelect (AC.InvItem invItem) (at Assets/HotspotHighlighter.cs:69)
AC.EventManager.Call_OnChangeInventory (AC.InvCollection invCollection, AC.InvInstance invInstance, AC.InventoryEventType inventoryEventType, System.Int32 amountOverride) (at Assets/AdventureCreator/Scripts/Managers/EventManager.cs:1602)
AC.RuntimeInventory.SelectItem (AC.InvInstance invInstance, AC.SelectItemMode _mode) (at Assets/AdventureCreator/Scripts/Inventory/RuntimeInventory.cs:216)
AC.InvInstance.Use (System.Boolean selectIfUnhandled) (at Assets/AdventureCreator/Scripts/Inventory/InvInstance.cs:414)
AC.MenuInventoryBox.HandleDefaultClick (AC.MouseState _mouseState, System.Int32 _slot) (at Assets/AdventureCreator/Scripts/Menu/Menu classes/MenuInventoryBox.cs:1306)
AC.RuntimeInventory.ProcessInventoryBoxClick (AC.Menu _menu, AC.MenuInventoryBox inventoryBox, System.Int32 _slot, AC.MouseState _mouseState) (at Assets/AdventureCreator/Scripts/Inventory/RuntimeInventory.cs:1100)
AC.MenuInventoryBox.ProcessClick (AC.Menu _menu, System.Int32 _slot, AC.MouseState _mouseState) (at Assets/AdventureCreator/Scripts/Menu/Menu classes/MenuInventoryBox.cs:2389)
AC.MenuElement.ProcessClickUI (AC.Menu _menu, System.Int32 _slot, AC.MouseState _mouseState) (at Assets/AdventureCreator/Scripts/Menu/Menu classes/MenuElement.cs:268)
AC.MenuInventoryBox.ProcessClickUI (AC.Menu _menu, System.Int32 _slot, AC.MouseState _mouseState) (at Assets/AdventureCreator/Scripts/Menu/Menu classes/MenuInventoryBox.cs:335)
AC.MenuElement+<>c__DisplayClass38_0.b__0 () (at Assets/AdventureCreator/Scripts/Menu/Menu classes/MenuElement.cs:213)
UnityEngine.Events.InvokableCall.Invoke () (at /Users/bokken/build/output/unity/unity/Runtime/Export/UnityEvent/UnityEvent.cs:178)
UnityEngine.Events.UnityEvent.Invoke () (at /Users/bokken/build/output/unity/unity/artifacts/generated/UnityEvent/UnityEvent_0.cs:57)
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:530)"

I am placing in some screenshots of the C# code in question.

Comments

  • the parts in red throw the error. I am not sure why the same audio source works for parts of the script, but not for other parts. This leads me to suspect that the event manager stuff is somehow messed up after transitioning from scene to another.

  • Your script registers itself with the events when the scene starts, but doesn't appear to un-register itself when the scene ends. The Event Manager will attempt to call your script's "itemHover" function, but it won't be able to find it once in the new scene.

    To avoid this, use the OnEnable and OnDisable functions to register and un-register your events:

    void OnEnable ()
    {
        EventManger.OnInventorySelect += itemHover;
        // etc
    }
    
    void OnDisable ()
    {
        EventManger.OnInventorySelect -= itemHover;
        // etc
    }
    
  • Ah, I had no idea this needs to be done. Thanks!

  • Yep, this fixed it! Thanks a bunch!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.