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

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

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:

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.

1 Like

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 Louis propose choosing random item from List

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

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();
}
}

3 Likes

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:

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

if something like this happens you need to check Microsoft docs 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

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

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

thx for the help <3

2 Likes