Animation clip looping issue

I am using a .glb model which has embedded animation clips into it. Using importer to load that model into a scene at runtime and also fetching and playing animation clip at runtime (all the process of loading and animation is working fine) . But the issue here is the animation clip is not playing loop. Those clips loops when I manually drag and drop any of those animation clip onto the model gameobject into the scene but not when I try to play them with this script :-

void OnLoad(GameObject model, AnimationClip[] anims)
    {
        // Creates the controller
        animator = model.AddComponent<Animator>();

        AnimationClip currentclip = anims[2];

        int hash = Animator.StringToHash(currentclip.name);


        currentclip.wrapMode = WrapMode.Loop; //setting loop true
        animator.runtimeAnimatorController = AnimatorController.CreateAnimatorControllerAtPathWithClip("Assets/MyAnim.controller", currentclip);

        AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
        Debug.Log("Is animation clip on loop" + currentclip.isLooping); //returning false
        Debug.Log("Is animation state on loop" + stateInfo.loop); //returning false
        animator.Play(hash);
    }

@apoorvasharma2397 - It appears you are correctly trying to set the animation clip’s wrap mode to loop. However, from Unity version 5 onwards, the AnimationClip.isLooping property is Read-Only. This means you can’t change the loop property of an animation clip at runtime as you’re attempting to do here.

Instead, you’ll need to create a new AnimationClip instance, copy the old clip’s keyframes to the new one, and set the loop properties on the new clip. This has to be done at edit time, not at runtime.

However, there’s an alternative approach that could work for you. You can create an AnimatorOverrideController and use that to loop your animation clip.

Something like this (Warning: Untested)

void OnLoad(GameObject model, AnimationClip[] anims)
{
    animator = model.AddComponent<Animator>();

    AnimationClip currentclip = anims[2];
    int hash = Animator.StringToHash(currentclip.name);

    AnimatorOverrideController overrideController = new AnimatorOverrideController();
    overrideController.runtimeAnimatorController = animator.runtimeAnimatorController;

    var clips = new List<KeyValuePair<AnimationClip, AnimationClip>>();
    foreach (var originalClip in overrideController.animationClips)
    {
        if (originalClip == currentclip)
        {
            var newClip = Object.Instantiate(originalClip);
            newClip.wrapMode = WrapMode.Loop;
            clips.Add(new KeyValuePair<AnimationClip, AnimationClip>(originalClip, newClip));
        }
    }

    overrideController.ApplyOverrides(clips);
    animator.runtimeAnimatorController = overrideController;

    animator.Play(hash);
}

Here, we are creating an AnimatorOverrideController and setting its runtimeAnimatorController to the one we have on our animator. Then, we iterate over each animation clip in the override controller. If we find our current animation clip, we create a new clip that is a clone of our original clip, set its wrap mode to loop, and add it to the list of overridden clips. Finally, we apply these overrides to the controller and set the animator’s runtimeAnimatorController to our override controller.

Hope this helps solve your looping issue!