# Setting Initial parameter values by script

**URL:** <https://qa.fmod.com/t/setting-initial-parameter-values-by-script/22464>\
**Category:** Unity\
**Created:** [December 31, 2024, 6:26pm UTC](https://qa.fmod.com/t/setting-initial-parameter-values-by-script/22464 "2024-12-31T18:26:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wwu39](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@wwu39](https://qa.fmod.com/u/wwu39)\
**Post date:** [December 31, 2024, 6:26pm UTC](https://qa.fmod.com/t/setting-initial-parameter-values-by-script/22464/1 "2024-12-31T18:26:26Z")

</div>

![inspector](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/7/7653d7e200adb9d7cae70ce4886cdc0fd6d70fb1.png)  
Is there a way to set initial parameter values by script? I tried SetParameter but it only works when the event is currently playing. I need to set it when the actor is spawning. I can’t do it in inspector because the value being set is from some file.  
Part of my code:  
 ![code](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/e/e9cec1013eb87f9589e11435e30dadc2cce1a25b.png)

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [January 2, 2025, 12:36am UTC](https://qa.fmod.com/t/setting-initial-parameter-values-by-script/22464/2 "2025-01-02T00:36:54Z")

</div>

Hi,

An option may be rather than using the Emitter instance, if you create the instance yourself:

```auto
private FMOD.Studio.EventInstance inst;

void Start()
{
	inst = FMODUnity.RuntimeManager.CreateInstance("event:/Sound/" + d.moveSOund);
	if (!inst.isValid())
	{
		Debug.Log("Inst is not valid yet");
	}
	else
	{
		Debug.Log("Inst is valid right after creating it");
		FMOD.RESULT result = inst.setParameterByName("Fraction", (int)d.fraction, false);
		if (result != FMOD.RESULT.OK)
		{
			Debug.Log($"Failed to set param with result {result}");
		}
		else
		{
			Debug.Log("Successfully set the parameter without playing the event");
		}
	}
}

```

Unfortunately, as the emitter does not create the instance till it is about to be played it will not be valid for the parameter to be set.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![wwu39](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@wwu39](https://qa.fmod.com/u/wwu39)\
**Post date:** [January 2, 2025, 2:49pm UTC](https://qa.fmod.com/t/setting-initial-parameter-values-by-script/22464/3 "2025-01-02T14:49:28Z")

</div>

Do I need to free/destroy the EventInstance when the gameobject is destroyed?

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [January 2, 2025, 7:57pm UTC](https://qa.fmod.com/t/setting-initial-parameter-values-by-script/22464/4 "2025-01-02T19:57:34Z")

</div>

Yes, you can call `inst.release()` right after it is started so that when the event stops it is cleaned up automatically. More info can be found here: [FMOD Engine | Studio API Reference - Studio::EventInstance](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_start)

---

<div class="post-metadata">

**Author:** ![wwu39](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@wwu39](https://qa.fmod.com/u/wwu39)\
**Post date:** [January 3, 2025, 8:27pm UTC](https://qa.fmod.com/t/setting-initial-parameter-values-by-script/22464/5 "2025-01-03T20:27:38Z")

</div>

😆 haha I’m actually curious on how emitter’s “Initial Parameter Values” works in the inspector.

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [January 6, 2025, 11:12pm UTC](https://qa.fmod.com/t/setting-initial-parameter-values-by-script/22464/6 "2025-01-06T23:12:24Z")

</div>

Ah sorry for the confusion.

In the `StudioEventEmitter.cs` script in the `PlayInstance()` function, after the instance is created it will loop through a list of `Params` like we were doing when creating the instance our self:

```auto
foreach (var param in Params) // Line 297
{
    instance.setParameterByID(param.ID, param.Value);
}

```

Hope this helps!
