# How to change the loop state before playback using Unity code?

**URL:** <https://qa.fmod.com/t/how-to-change-the-loop-state-before-playback-using-unity-code/22651>\
**Category:** Unity\
**Created:** [March 3, 2025, 7:30am UTC](https://qa.fmod.com/t/how-to-change-the-loop-state-before-playback-using-unity-code/22651 "2025-03-03T07:30:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![twocomet](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@twocomet](https://qa.fmod.com/u/twocomet)\
**Post date:** [March 3, 2025, 7:30am UTC](https://qa.fmod.com/t/how-to-change-the-loop-state-before-playback-using-unity-code/22651/1 "2025-03-03T07:30:30Z")

</div>

I know that in FMOD Studio, you can set up a loop by using **Add Loop Region**.

However, if the loop is not set in FMOD Studio, I would like to know how to enable or disable looping directly through Unity code.

Currently, I am playing the sound like this:  
I will paste part of the code below.

```auto
private FMOD.Studio.EventDescription eventDescription;

if (eventDescription.isValid())
        {
            instance = GetInstance();
            await WaitForChannelGroup(instance);

            if (instance.isValid() && is3D)
            {
#if UNITY_PHYSICS_EXIST
                if (target.GetComponent<Rigidbody>())
                {
                    Rigidbody rigidBody = target.GetComponent<Rigidbody>();
                    RuntimeManager.AttachInstanceToGameObject(instance, target, rigidBody);
                    instance.set3DAttributes(RuntimeUtils.To3DAttributes(target, rigidBody));
                }
                else
#endif
#if UNITY_PHYSICS2D_EXIST
                if (target.GetComponent<Rigidbody2D>())
                {
                    var rigidBody2D = target.GetComponent<Rigidbody2D>();
                    RuntimeManager.AttachInstanceToGameObject(instance, target, rigidBody2D);
                    instance.set3DAttributes(RuntimeUtils.To3DAttributes(target, rigidBody2D));
                }
                else
#endif
                {
                    RuntimeManager.AttachInstanceToGameObject(instance, target);
                    instance.set3DAttributes(RuntimeUtils.To3DAttributes(target));
                }
            }
if (instance.isValid())
            instance.start();

        }

```

---

<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:** [March 4, 2025, 12:25am UTC](https://qa.fmod.com/t/how-to-change-the-loop-state-before-playback-using-unity-code/22651/2 "2025-03-04T00:25:24Z")

</div>

Hi,

An option may be using [FMOD Engine | Studio API Reference - EventInstance::setTimelinePosition](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_settimelineposition).  
Something like this:

```auto
if (eventInstance.isValid())
{
    FMOD.RESULT result = eventInstance.getTimelinePosition(out int position);
    if (result == FMOD.RESULT.OK)
    {
        if (position >= 10000 /* This is the target loop point */)
        {
            result = eventInstance.setTimelinePosition(0); /* Loop back to the start of the event */
            if (result != FMOD.RESULT.OK)
            {
                Debug.LogError($"Failed to loop event with error: {result}");
            }
        }
    }
    else
    {
        Debug.LogError($"Failed to get timeline position: {result}");
    }
}

```

However, these isn’t a way to create a new FMOD Studio loop region from within Unity.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![twocomet](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@twocomet](https://qa.fmod.com/u/twocomet)\
**Post date:** [March 9, 2025, 8:51am UTC](https://qa.fmod.com/t/how-to-change-the-loop-state-before-playback-using-unity-code/22651/3 "2025-03-09T08:51:43Z")

</div>

Thank you for your reply.  
I don’t want to adjust the loop region.  
There are some audio clips that I want to keep playing in Unity with looping enabled, even if no loop settings were made in the studio.  
I would like to configure this via code.  
Is it impossible?

---

<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:** [March 11, 2025, 6:34am UTC](https://qa.fmod.com/t/how-to-change-the-loop-state-before-playback-using-unity-code/22651/4 "2025-03-11T06:34:12Z")

</div>

I see, thank you for the clarification.

An option could be using the event callback [FMOD Engine | Studio API Reference - FMOD\_STUDIO\_EVENT\_CALLBACK\_STOPPED](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html#fmod_studio_event_callback_type:~:text=playing.%20Parameters%20%3D%20unused.-,FMOD_STUDIO_EVENT_CALLBACK_STOPPED,-The%20event%20has) and restarting the event instance from there? We have scripting example implementing a callback here: [Unity Integration | Scripting Examples - Programmer Sounds](https://fmod.com/docs/2.02/unity/examples-programmer-sounds.html).

Hope this helps
