Hey! So long story short I’m generating some UI buttons based on the legacy animations assigned to an object. I’m using Piglet to load these gltf models at runtime (fantastic Asset!), but whenever I click on one of the generated buttons, I get the error below:
Error Message
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <9577ac7a62ef43179789031239ba8798>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <9577ac7a62ef43179789031239ba8798>:0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <9577ac7a62ef43179789031239ba8798>:0)
RiggedObjectProperties+<>c__DisplayClass8_0.<AssignReferences>b__0 () (at Assets/Scripts/Object Properties/RiggedObjectProperties.cs:45)
UnityEngine.Events.InvokableCall.Invoke () (at <0847a0faf94444ccbaf1958021b27f54>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <0847a0faf94444ccbaf1958021b27f54>:0)
UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Hub/Editor/2019.4.17f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.4.17f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.4.17f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2019.4.17f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
VRInputModule:processRelease(PointerEventData) (at Assets/Scripts/UIControllers/VRInputModule.cs:115)
VRInputModule:Process() (at Assets/Scripts/UIControllers/VRInputModule.cs:73)
UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2019.4.17f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)
[ICODE][/ICODE]
My code for doing this is here:
Because of the way the Piglet Asset loads in the animations, I’m getting the animation names through an “AnimationList” component, then passing those strings into the Unity Animation component to play them.
Code
void generateButtons()
{
animationlist = objectManager.selectedObject.GetComponent<AnimationList>();
animationComponent = objectManager.selectedObject.GetComponent<Animation>();
animationComponent.playAutomatically = true;
animationComponent.Play();
for (int i = 0; i < animationlist.Clips.Count; i++)
{
Button animationButton = Instantiate(animationSelectionButton, Vector3.zero, Quaternion.Euler(0, 0, 0));
animationButton.GetComponentInChildren<TextMeshProUGUI>().text = animationlist.Clips[i].name;
// IM GETTING THE ERROR ON THE BELOW LINE. BUT I HAVE NO IDEA WHAT IT CAN BE :)
animationButton.onClick.AddListener(delegate { playAnimation(animationlist.Clips[i].name); }); // METHOD BELOW.
animationButton.gameObject.SetActive(true);
}
}
// Then this is what I'm trying to assign to each button:
void playAnimation(string animationName)
{
Debug.Log("Playing " + animationName + " on object: " + objectManager.selectedObject.name);
animationComponent.Play(animationName);
}
I hope that makes sense! Thanks so much!