Question by
rosannacap29 · Nov 30, 2020 at 05:35 PM ·
animationscripting problemcoroutinebuttons
How can I stop, start and reset a coroutine?
Hello, I have 645 objects that are animated using the following script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrayTest : MonoBehaviour
{
public GameObject[] numbers;
// Start is called before the first frame update
void Start()
{
}
bool attractorMode = true, isRunning;
int currentIndex;
// Update is called once per frame
void Update()
{
// only invoke if the coroutine isn't already running
if (attractorMode && !isRunning) StartCoroutine(Cycle());
}
IEnumerator Cycle()
{
// these lines execute by the end of the frame in which Cycle() was invoked
isRunning = true;
numbers[currentIndex].SetActive(true);
yield return new WaitForSeconds(0.25f);
// execution will not return to the next statement until 0.25 seconds later
numbers[currentIndex].SetActive(true);
Destroy(numbers[currentIndex], 0.05f);
// increment the index and ensure it loops around at the end
currentIndex++;
if (currentIndex >= numbers.Length) currentIndex = 0;
isRunning = false;
}
}
Now the problem is that I have created some buttons: play, stop and reset.
How do I link these buttons to my animation script?
Sorry, but I'm a beginner with Unity.
Thanks to anyone who wants to help me.
Comment
Your answer
Follow this Question
Related Questions
2D Spine Animation not playing 0 Answers
skip a bit forward in current animation? 0 Answers
How do I get my animated Object to switch animations on Collider? 1 Answer
UI Button Movement Problem 0 Answers
animation scirpting 1 Answer