# Q U E S T I O N II Unity Scripts I Random Music in the Background?

**URL:** <https://qa.fmod.com/t/q-u-e-s-t-i-o-n-ii-unity-scripts-i-random-music-in-the-background/20752>\
**Category:** Unity\
**Tags:** unity, csharp\
**Created:** [October 9, 2023, 7:21am UTC](https://qa.fmod.com/t/q-u-e-s-t-i-o-n-ii-unity-scripts-i-random-music-in-the-background/20752 "2023-10-09T07:21:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iAgharta](https://avatars.discourse-cdn.com/v4/letter/i/ea666f/32.png) [@iAgharta](https://qa.fmod.com/u/iAgharta)\
**Post date:** [October 9, 2023, 7:21am UTC](https://qa.fmod.com/t/q-u-e-s-t-i-o-n-ii-unity-scripts-i-random-music-in-the-background/20752/1 "2023-10-09T07:21:25Z")

</div>

Hi guys.

Im working on a project with fmod and unity and I was wondering if is possible to add some sort of randomness music events in one scene, let me explain further:

like Minecraft when you´re playing the game, songs trigger randomly in the back, i want to add some sort of thing in my scene which run the gameplay.

In FMOD I have my interactive music already set up, every track in a different event with some unique parameters to every event. My question: is there some sort of script to play a random event with all his parameters from a list you called???

i hope i explained well English is not my first language D:

music to the scene which run the gameplay

---

<div class="post-metadata">

**Author:** ![Leah\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/leah_fmod/32/3685_2.png) [@Leah\_FMOD](https://qa.fmod.com/u/Leah_FMOD)\
**Post date:** [October 10, 2023, 11:24pm UTC](https://qa.fmod.com/t/q-u-e-s-t-i-o-n-ii-unity-scripts-i-random-music-in-the-background/20752/2 "2023-10-10T23:24:59Z")

</div>

Hi,

Unfortunately there’s no existing script in the FMOD for Unity plugin that provides exactly what you need, but it’s entirely possible to create one yourself.

From your brief description, I would recommend the following:

- Store a publicly exposed or serialized `EventReference` in your script that you can assign your events to via the Unity Inspector
- Create an instance of each event using [`FMODUnity.RuntimeManager.createInstance()`](https://fmod.com/docs/2.02/unity/api-runtimemanager.html#createinstance), and store them in a list
- When you need to play an event, randomly pick an event to play from your list of events, set parameters accordingly, and start the event with [`Studio.EventInstance.start()`](https://www.fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_start)
- If you want to start another event when the current one as finished, you can query the playback state of the current event with [`Studio.EventInstance.getPlaybackState()`](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_getplaybackstate)
- Finally, make sure to release all of your created `EventInstance`s with [`Studio.EventInstance.release()`](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_release) when you’re done with them. [`MonoBehaviour.OnDestroy`](https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDestroy.html) is a good place to do this, for example

This is a very general outline - if you can provide some more specific information on how your events are set up and the desired behavior, I may be able to provide more detailed guidance.

---

<div class="post-metadata">

**Author:** ![doudar41](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/doudar41/32/5163_2.png) [@doudar41](https://qa.fmod.com/u/doudar41)\
**Post date:** [October 11, 2023, 12:09pm UTC](https://qa.fmod.com/t/q-u-e-s-t-i-o-n-ii-unity-scripts-i-random-music-in-the-background/20752/3 "2023-10-11T12:09:47Z")

</div>

As I understand, you have several music events. Each of them is interactive. For example, event has parameters to change music from idle to fight or explore to suspence. You want to start one of events randomly and use these parameters to control music in game.  
If I understand right. It’s good to have the same names of parameters you want to use in a game in each of those events. So, you can load different events but apply the same functions to change parameters.  
yoursoundinstance.SetParameterByName(“Youparameter”, floatvalue)  
Then you can use logic that Leah propose choosing random item from [List](https://www.tutorialspoint.com/how-to-select-a-random-element-from-a-chash-list)

Simple one can looks like this. I didn’t check it may have some mistakes.

```auto
using FMODUnity;

public EventReference[] EventNames;
List<FMOD.Studio.EventInstance> soundInstances = new List<FMOD.Studio.EventInstance>();
int randomIndex = 0 ;

private void Start()
{
foreach (EventReference event in EventNames)
{
soundInstances.Add(RuntimeManager.CreateInstance(event));
}
}

public void StartRandomEvent()
{
randomIndex = random.Next(soundInstances.Count);
soundInstances[randomIndex].start();
}

public void ChangeParameter(float amount)
{
soundInstances[randomIndex].SetParameterByName("Parameter" , amount);
}

OnDestroy()
{
foreach (FMOD.Studio.EventInstance instance in soundinstances)
{
instance.release();
}
}

```

---

<div class="post-metadata">

**Author:** ![iAgharta](https://avatars.discourse-cdn.com/v4/letter/i/ea666f/32.png) [@iAgharta](https://qa.fmod.com/u/iAgharta)\
**Post date:** [October 11, 2023, 4:55pm UTC](https://qa.fmod.com/t/q-u-e-s-t-i-o-n-ii-unity-scripts-i-random-music-in-the-background/20752/4 "2023-10-11T16:55:56Z")

</div>

Thx for this, its exactly what i wanted…

Just some questions if I may, ur script show some error and i try to fix by doing this:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/8/8cb426a404517d5119fe4da16a8020131464cd65.png)

but in the StartRandomEvent() i cannot figured it out, the problem say “the name “random” does not exist in the curret context”

any ideas?D:

thx for the reply

---

<div class="post-metadata">

**Author:** ![doudar41](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/doudar41/32/5163_2.png) [@doudar41](https://qa.fmod.com/u/doudar41)\
**Post date:** [October 11, 2023, 6:42pm UTC](https://qa.fmod.com/t/q-u-e-s-t-i-o-n-ii-unity-scripts-i-random-music-in-the-background/20752/5 "2023-10-11T18:42:03Z")

</div>

if something like this happens you need to check [Microsoft docs](https://learn.microsoft.com/en-us/dotnet/api/system.random.next?view=net-7.0) and check what library specific command is using. In this case you need to add  
“using System;”  
Next thing, I’d recommend you to simplify system to one World to check if system works then add multiple arrays.  
Sorry, it was my mistake. For now in the Start method foreach loop has no body and you need a variable name inside () different from your array name, lets call it worldEvent. You need also to add brackets { } and if you need to add events to soundinstances list. It should looks something like this

```auto
foreach(EventReference worldEvent in World01)
{
FMOD.Studio.EventInstance instance = RuntimeManager.CreateInstance(event);
soundInstances.Add(instance);
}

```

---

<div class="post-metadata">

**Author:** ![iAgharta](https://avatars.discourse-cdn.com/v4/letter/i/ea666f/32.png) [@iAgharta](https://qa.fmod.com/u/iAgharta)\
**Post date:** [October 12, 2023, 9:59am UTC](https://qa.fmod.com/t/q-u-e-s-t-i-o-n-ii-unity-scripts-i-random-music-in-the-background/20752/6 "2023-10-12T09:59:40Z")

</div>

for anyone interested I was able to work it out with this method in this other post…

thx for the help \<3

> [@QUESTION ll Randon Event everytime the scene load](https://qa.fmod.com/t/question-ll-randon-event-everytime-the-scene-load/20758/):
>
> Hi guys. I want to make the gameplay music of my game more random, so every time the scene load, a different event with the music in it plays. So far I being able to create some sort of script to create a list in which I can drag others gameObjects with the fmod emitter component inside and the music I choose. this is the list in which i can drag the slaves gameObjects So far the sound is working, every event is playing at the same time, my question is: is there a way to play on si…
