IMAGES SPEED PROBLEM

the image load to fast when i click button , can you help me ? thank best regard

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Sprites;
using UnityStandardAssets.CrossPlatformInput;


public class LoadSprite : MonoBehaviour
{

   public int i = 0;
   public Sprite[] gallery ;


    public IEnumerator ThayAnh()
    {
        Image anhUI = this.GetComponent<Image>();

        if (CrossPlatformInputManager.GetButton("Next"))
        {
            if (i + 1 < gallery.Length)
            {

                yield return new WaitForSeconds(0.5f);
                i++;
            }
            anhUI.sprite = gallery[i];
        }
        if (CrossPlatformInputManager.GetButton("Pre"))
        {
            if (i - 1 > 0)
            {
                yield return new WaitForSeconds(0.5f);
                i--;

            }
            anhUI.sprite = gallery[i];
        }
    }
    void Update()
    {
        StartCoroutine( ThayAnh());
    }

}

You are starting a new coroutine every frame so you have multiple instances of it running. Move the input checks to update and change the images when they are true there.

Other than that, what is the problem with how fast the images changes?

1 Like

thank you for your reply , i make a video for my problem ,i dont know how to fix , please help me

https://www.youtube.com/watch?v=nQSvjg47GfQ

i want to make an interactive app like this

solved

float timeLeft = 0.5f;
void Update()
{
timeLeft -= Time.deltaTime;
if(timeLeft < 0)
{
//myfunction();
//reset timer
}
}