The image sequence of an array is moving too fast

I’ve made a c# script which loads an image sequence thus producing an effect of a video being played.

The video is moving too fast. No matter what adjustments are done to the code, it doesn’t react.

using UnityEngine;
using System.Collections;

public class animation : MonoBehaviour {

public Texture2D[] images;
private int i=0;
public float currentImageTime = 0f;
public AudioClip videoSound;
public bool videoIsPlaying = false;
public float regulateSpeed = 0f;

void Start()
{
	if(videoIsPlaying==true)
	audio.PlayOneShot(videoSound);
}

void Update ()
{
	currentImageTime += Time.deltaTime*10f;
	if(i<images.Length)
	{
	ChangeImage();
	videoIsPlaying = true;
	}
	else
	{
	videoIsPlaying = false;
	}
}

void OnGUI() 
{
	i++;
    GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), images*);* 
  • }*

  • void ChangeImage()*

  • {*

  •   if(currentImageTime >= regulateSpeed)*
    
  •   { 	*
    
  •   	OnGUI ();*
    
  •   	currentImageTime = 0f;*
    
  •   }*
    
  •   else{}*
    
  • }*
    }

The OnGUI() functions is called at each frame by Unity itself, it’s not a function you should call yourself. You should not call it in ChangeImage() its wrong code. Instead do i++ in the ChangeImage.

void ChangeImage()

{

  if(currentImageTime >= regulateSpeed)
  {

     i++;

     currentImageTime -= regulateSpeed;

  }

}

And of course remove the i++ from OnGUI.

there`s a function called WaitForSeconds

so load your i image and after that use this function to wait for ,let`s say 1 sec.

Also, for many peeps who have a similar problem, find this hoping for an answer and then only see “I fixed it”, but not the actual answer - the code is fine, once you divide the videoTime from imageAmount, thus giving how much time each frame has t be shown. The problem is - it’s not the exact amount of frames as in the video, so a bit of fluent sync will be lost. If it doesn’t sync no matter how many times you’ve played with the numbers - change amount of the frames, etc.