FMOD parameter not changing through C# script

376 views Asked by At

I'm currently trying to develop my first mini game (a VR musical experience).

I'm trying to have the value of a slider control an FMOD parameter, but nothing happens. Plus the function does not show in the On Value Changed of the slider...

I'm already using that slider to control the transparency of a material and if I try changing the FMOD parameter manually in the inspector it works just fine.

Here is the script:

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

      
    }

    // Update is called once per frame
    void Update()
    {
        SetBassVolume();
    }

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

Thanks in advance for the help! :)

1

There are 1 answers

6
iFralex On

The problem is that you have to add the function in the onValueChanged () listener. To do it:

  • Select the gameObject with the Slider component;
  • Click the "+" button in the OnValueChanged () listener below;
  • In the empty field, under "Runtime only", drag the gameObject with this script;
  • Click the box that now has the name "No Function";
  • In the component list, place the cursor over the script name, and under the "Dynamic parameters" category, the SetBassVolume () method.

But first you should tweak the method a bit, like so:

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

Good work!