Greetings,
I’m trying to write a code to change the value of a parameter, based on an in-game event.
I already have a Studio Event Emitter script on my FPSController (Unity’s default controller) and I’m trying to create a script on my enemy to trigger different music when it detects the player:
You can’t get the Studio Event Emitter from the FPSController by creating a new Event Instance in another script. In simple terms, it’s like creating a new copy of the instance (that is not playing) with its own parameter instance. You should try to get a reference to the Studio Event Emitter already playing. Try something like this:
[SerializeField]
private FMODUnity.StudioEventEmitter emitter;
void Update()
{
if (playerDetected)
{
SetParameter(emitter.EventInstance, "Enemy", 1.0f);
}
}
void SetParameter(FMOD.Studio.EventInstance e, string name, float value)
{
FMOD.Studio.ParameterInstance parameter;
e.getParameter(name, out parameter);
parameter.setValue(value);
}
Drag the GameObject with the FPSController/Studio Event Emitter to the empty Studio Event Emitter field in your Enemy’s inspector tab. By doing this, you get a reference to that specific Event Emitter. If you notice, we pass the Event Instance of the emitter to the SetParameter method : )