Cant store EventInstances in the list

Hi.
Im trying to store the EventInstance in the list after creation of the event , but error pops out NullReferenceException . I can start the event and it starts playing , but after i cant add it to the list.

Here is example of code:

            EventInstance audioEvent = CreateInstance(FMOD_Events.GetEventReference(audioName, FMOD_Events.Music));

            PlayLoop(audioEvent);

            eventInstances.Add(audioEvent);

Hi,

What version of the FMOD Unity integration are you using?

Could I get the full script to test on my end?

I was able to add events to a list using the following code:

private List<FMOD.Studio.EventInstance> eventList = new List<FMOD.Studio.EventInstance> ();
// Start is called before the first frame update
void Start()
{
    eventList.Add(FMODUnity.RuntimeManager.CreateInstance($"event:/Music/Level 01"));
    eventList.Add(FMODUnity.RuntimeManager.CreateInstance($"event:/Music/Level 02"));

    for (int i = 0; i < eventList.Count; i++)
    {
        eventList[i].start();
        eventList[i].release();
    }
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        for (int i = 0;i < eventList.Count;i++)
        {
            eventList[i].stop(STOP_MODE.IMMEDIATE);
        }
    }
}

Hope this helps.

1 Like

Wherever you have declared the list, make sure it is not null.

As Connor’s code has:
private List<FMOD.Studio.EventInstance> eventList = new List<FMOD.Studio.EventInstance> ();

So the ... = new List<...>(); is the important part here, and not having it is probably causing the NullReferenceException.

2 Likes