Triggering an Attached One-Shot giving null reference even when an event is attached?

Hey all. Very new to this so bear with me!

I’ve been using the following script to attach audio to objects to be triggered on awake:

public class TriggerAudio : MonoBehaviour
{
    [FMODUnity.EventRef]
    public string Event;
    public bool PlayOnAwake;

    public void PlayOneShot()
    {
        FMODUnity.RuntimeManager.PlayOneShotAttached(Event, gameObject);
    }

    private void OnEnable()
    {
        if (PlayOnAwake)
            PlayOneShot();
    }
}

This works a treat.

I’m trying to apply the same logic to a set of Items that all have discrete prefabs but share one script. I want to use the Event pane made above to allow me to load individual events to each object, but trigger them from the same script.

I have set up the Event Pane exactly as in the case above, using the following:

[FMODUnity.EventRef]                                          
    public string AudioEvent;

    public void PlayOneShot()                                      
    {
        FMODUnity.RuntimeManager.PlayOneShotAttached(AudioEvent, gameObject);
    }

In this case, the sound needs to be triggered when the player interacts with the Item, and at the moment we have the following set up:

 public void Interact(MyPlayer myPlayer)
    {
        if(myPlayer == null)
        {
            Debug.LogWarning("Parameter 'myPlayer' is null");
            return;
        }

        //Play Interact Sound
        PlayOneShot();

        //Add the item to the picker's inventory.
        myPlayer.InventoryController.AddToInventory(ItemDatabase.instance.GetItemId(item), amount);
        
        myDestroyer = myPlayer.PlayerObject.GetComponent<PhotonView>();

For some reason, the ‘PlayOneShot();’ that worked perfectly in the first example now causes a Null Reference from FMOD. I have no idea what it means:

NullReferenceException: Object reference not set to an instance of an object
FMODUnity.RuntimeManager.PathToGUID (System.String path) (at Assets/Plugins/FMOD/src/Runtime/RuntimeManager.cs:1031)
FMODUnity.RuntimeManager.PlayOneShotAttached (System.String path, UnityEngine.GameObject gameObject) (at Assets/Plugins/FMOD/src/Runtime/RuntimeManager.cs:1103)
ItemObject.PlayOneShot () (at Assets/Scripts/Inventory/ItemObject.cs:22)
ItemObject.Interact (MyPlayer myPlayer) (at Assets/Scripts/Inventory/ItemObject.cs:38)
Interact.CheckAimedItem () (at Assets/Scripts/Player/Interact.cs:92)
Interact.Update () (at Assets/Scripts/Player/Interact.cs:60)

It also prevents the rest of the script from executing, so the item is not picked up and added to the player’s inventory

Any help would be really appreciated

My best bet would be that your AudioEvent variable has no value, or an incorrect one. Did you check all your ItemObject to make sure every one of them has the value you want in their AudioEvent field? In your PlayOneShot function, you can try logging the value of AudioEvent before calling PlayOneShotAttached.

There’s definitely an event attached to the Item, so that can’t be what’s causing it. Seems like the error is in the PlayOneShot(); function, since removing that gets rid of the error and allows the rest of the script to execute as normal, just without playing the sound

Did you still try to log the value of AudioEvent before the call to PlayOneShotAttached, just in case?

I’ve been able to repoduce this by passing null as the path parameter in RuntimeManager.PlayOneShotAttached. Following @Grhyll’s suggestion, try adding another safety check to make sure AudioEvent definitely has a non-null value.
If it’s still throwing after doing that, if you could please let me know which versions of FMOD and Unity you are running then I can investigate further and see if there’s a deeper underlying issue here.