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