Can't get C# script to change an FMOD Parameter?

Hi,

I’ve been trying to have a slider in Unity control a parameter in FMOD.
This is the first script that I tried:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class FMODParameters : MonoBehaviour
{

    FMODUnity.StudioEventEmitter changeVolume;

    FMOD.Studio.EventInstance bass;

    public string bassVolumeParameter;

    public Slider slider;

    

    void Start()
    {
        changeVolume = GetComponent<FMODUnity.StudioEventEmitter>();

        bass = FMODUnity.RuntimeManager.CreateInstance("event:/Beat/2D/2D Bass");

        slider = GetComponent<Slider>();

      
    }

   
   

    void SetBassVolume(float _value)
    {
        bass.setParameterByName(bassVolumeParameter, slider.value / 250);
        
    }
}

But then, the OnValueChanged of the slider in unity wouldn’t show the dynamic parameters so I couldn’t really use it.

So then I have tried this one:

[FMODUnity.EventRef]
public string NAME EVENT = “event:/EVENT”;
public FMOD.Studio.EventInstance AUDIO EVENT;
public FMOD.Studio.ParameterInstance PARAMETER EVENT;

void Start () {
AUDIO EVENT = FMODUnity.RuntimeManager.CreateInstance(NAME EVENT);
AUDIO EVENT.getParameter(“NAME PARAMETER FMOD”, out PARAMETER EVENT);
AUDIO EVENT.start();
}

void OnTriggerEnter(Collider other)
{
PARAMETER EVENT.setValue(VALUE IN FLOAT);
}

which I’ve found on this forum but it gives me the error CS0234: The type or namespace name ‘ParameterInstance’ does not exist in the namespace ‘FMOD.Studio’ (are you missing an assembly reference?)
and CS1061: ‘EventInstance’ does not contain a definition for ‘getParameter’ and no accessible extension method ‘getParameter’ accepting a first argument of type ‘EventInstance’ could be found (are you missing a using directory or assembly reference?)

I’m a bit of a noob with coding, so anyone has encountered any of these issues before?
Alternatively, what’s the simplest way to get a C# script to change an FMOD parameter?

Thanks in advance for the help!

Hi,

Your first attempt was really close! Just a few things were missing.

If You are trying to change the volume on FMODUnity.StudioEventEmitter changeVolume then what you want to call is:


changeVolume.EventInstance.setParameterByName("YourParam", slider.value);

This will change the volume on the parameter on the changeVolume variable.

If you are wanting the change the volume on FMOD.Studio.EventInstance bass there a few things to do first, you will want to do this in the Start() function:

Initializing bass EventInstance

// This part is perfect
	bass = FMODUnity.RuntimeManager.CreateInstance("event:/Beat/2D/2D Bass");

// We should check if the instance was created properly
// It is always a good idea to Debug check often 
	If (bass.isValid() != true)
	{
		Debug.Log("Failed to create bass instance");
	}
	
// FMOD.RESULT is a very useful tool for debugging most of FMOD's function calls, use it where ever you can. 
	FMOD.RESULT result; 
	
// We will now need to start the instance through code, as we have created the instance in code
	result = bass.start();
	
	If (result != FMOD.RESULT.OK)
	{
	// Now we check if the function ran correctly or if there was an error, we will know when it happened and what the result is.
		Debug.Log("Failed to start instance with the result of " + result); 
	}
		

// We call release so that when the sound is stopped the memory is freed
	result = bass.release(); 

Some things to fix in the SetBassVolume() function. It doesn’t require any parameters as we are getting the value from the slider which is a member variable. So we could just have the function set like this:

	Public void SetBassVolume()
	{
		bass.setParameterByName(bassVolumeParamter, slider.value); 
		
	// Alternatively if you want to change the value of `changeVolume` you would call 
		changeVolume.EventInstance.setParameterByName(bassVolumeParamter, slider.value); 
	// But that will depend on which instance you decide to use. 
	// Now when ever this function is called it will set either instances volume parameter to the same value as the `slider`
	}

This function could be attached to a button or called in Update() to check if it is working.

Really good examples of how to use the FMOD API can be found under Unity IntegrationScripting Examples | Basic. If you are ever confused about what the API is doing then Welcome to FMOD API is the best place to start looking!

Could you possibly link the forum post where you found that scripting example as well?

Hope this helps!