# When do I need to create instances

**URL:** <https://qa.fmod.com/t/when-do-i-need-to-create-instances/21484>\
**Category:** Unity\
**Tags:** unity, csharp\
**Created:** [April 11, 2024, 9:32am UTC](https://qa.fmod.com/t/when-do-i-need-to-create-instances/21484 "2024-04-11T09:32:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hardsmoke](https://avatars.discourse-cdn.com/v4/letter/h/e9a140/32.png) [@hardsmoke](https://qa.fmod.com/u/hardsmoke)\
**Post date:** [April 11, 2024, 9:32am UTC](https://qa.fmod.com/t/when-do-i-need-to-create-instances/21484/1 "2024-04-11T09:32:23Z")

</div>

Hello  
I need to create an instance at the very beginning of the game, in awake or start methods. But I’m having problems because after executing the CreateInstance method, the instance is still is not valid. But at the same time, if I call this code not at the beginning of the game, but manually after some time, then it is created perfectly. As far as I understand, I need to wait for some process related to fmod, but I don’t know which one.  
Thanks.

```auto
        [SerializeField] private StudioEventEmitter _eventEmitter;
        private EventInstance EventInstance => _eventEmitter.EventInstance;

        public void CreateInstance()
        {
            if (EventInstance.isValid() == false) // this condition is true
            {
                 _eventEmitter.CreateInstance();

                UnityEngine.Debug.Log($"IS VALID NOW: {EventInstance.isValid()}"); // it still false
            }
        }

       public void Awake()
      {
          CreateInstance();
      }

```

---

<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:** [April 11, 2024, 11:45pm UTC](https://qa.fmod.com/t/when-do-i-need-to-create-instances/21484/2 "2024-04-11T23:45:45Z")

</div>

Hi,

The `FMODUnity.StudioEventEmitter` does not have the `CreaateInstance()` function, so I am unsure what it is doing.

The [Studio Event Emitter](https://fmod.com/docs/2.02/unity/api-studioeventemitter.html) class should be creating a valid [Event Instance](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html) on its own given a valid event path.

You can choose when the event emitter begins playing its event using the given [Trigger Conditions](https://fmod.com/docs/2.02/unity/game-components.html#trigger-conditions), one of the options is `Object Start`, this may provide the functionality you are looking for.

An alternative option is to use an [EventReference](https://fmod.com/docs/2.02/unity/api-common.html#eventreference) which you can use to create an event instance with `FMODUnity.RuntimeManager.CreateInstance()` ([Unity Integration | Scripting API Reference - RuntimeManager](https://fmod.com/docs/2.02/unity/api-runtimemanager.html#createinstance)) which would allow you to have complete control over the event instance.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![hardsmoke](https://avatars.discourse-cdn.com/v4/letter/h/e9a140/32.png) [@hardsmoke](https://qa.fmod.com/u/hardsmoke)\
**Post date:** [April 12, 2024, 1:37am UTC](https://qa.fmod.com/t/when-do-i-need-to-create-instances/21484/3 "2024-04-12T01:37:38Z")

</div>

Hello,

> [@Connor\_FMOD](#):
>
> You can choose when the event emitter begins playing its event using the given [Trigger Conditions](https://fmod.com/docs/2.02/unity/game-components.html#trigger-conditions), one of the options is `Object Start`, this may provide the functionality you are looking for.

I can’t use Object Start because I have to wait for some of my processing before creating the instance. And I don’t need to just launch an event, before that I have to install a callback for the programmer, here is my event launch script:

> **Full method of starting event**
>
> ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/a/a54d83500243172d2472082f552df280aaa3223c.png)

> [@Connor\_FMOD](#):
>
> The `FMODUnity.StudioEventEmitter` does not have the `CreaateInstance()` function, so I am unsure what it is doing.

Here are all the screenshots of using the StudioEventEmitter script and the CreateInstance method. I’m sorry, this method was added by my colleague from another fmod script, I didn’t know about it, but here’s the code

> **Screenshot of method CreateInstance**
>
> ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/4/4ab2e296ac38d70c1a06e94457b4504ff1b751ff.png)

> [@Connor\_FMOD](#):
>
> An alternative option is to use an [EventReference](https://fmod.com/docs/2.02/unity/api-common.html#eventreference) which you can use to create an event instance with `FMODUnity.RuntimeManager.CreateInstance()` ([Unity Integration | Scripting API Reference - RuntimeManager](https://fmod.com/docs/2.02/unity/api-runtimemanager.html#createinstance)) which would allow you to have complete control over the event instance.

Most likely I can’t use FMODUnity.RuntimeManager.CreateInstance() because in conjunction with StudioEventEmitter I use SteamAudioSouce, which requires an emitter

> **Screenshot of SteamAudioSource with StudioEventEmitter in editor**
>
> ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/1/15bc45ff76a35ceb758ac453e9e8743ef62764bb.png)

---

<div class="post-metadata">

**Author:** ![hardsmoke](https://avatars.discourse-cdn.com/v4/letter/h/e9a140/32.png) [@hardsmoke](https://qa.fmod.com/u/hardsmoke)\
**Post date:** [April 12, 2024, 4:00am UTC](https://qa.fmod.com/t/when-do-i-need-to-create-instances/21484/4 "2024-04-12T04:00:31Z")

</div>

I fixed it just adding Action when description is initialized in start method

> **StudioEventEmitter script**
>
> ```auto
> //fields
> public event Action DescriptionInitialized;
> private bool _isDescriptionInitialized = false;
> 
> protected override void Start()
> {
> RuntimeUtils.EnforceLibraryOrder();
> if (Preload)
> {
> Lookup();
> eventDescription.loadSampleData();
> }
> 
> HandleGameEvent(EmitterGameEvent.ObjectStart);
> 
> // If a Rigidbody is added, turn off "allowNonRigidbodyDoppler" option
> #if UNITY_PHYSICS_EXIST
> if (AllowNonRigidbodyDoppler && GetComponent<Rigidbody>())
> {
> AllowNonRigidbodyDoppler = false;
> }
> #endif
> 
> _isDescriptionInitialized = true;
> DescriptionInitialized?.Invoke();
> }
> 
> public void MakeOnDescriptionIsInitialized(Action toMake)
> {
> if (_isDescriptionInitialized)
> {
> toMake?.Invoke();
> }
> else
> {
> toMake += () => DescriptionInitialized -= toMake;
> DescriptionInitialized += toMake;
> }
> }
> 
> ```

---

<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:** [April 12, 2024, 6:43am UTC](https://qa.fmod.com/t/when-do-i-need-to-create-instances/21484/5 "2024-04-12T06:43:39Z")

</div>

Hi,

Thank you for sharing the solution.
