0

Two Script is attached with my one scene in unity , in one script i used method invokerepeating when it is called , my second script which is attached with the buttons didn't work.

void Start()
{
    currentImage = 0;
    if (thread == null) {
        thread = new Thread (new ThreadStart (CallChangeImage));
        thread.IsBackground = true;
        thread.Start ();
    }
}

public void CallChangeImage()
{
    InvokeRepeating("ChangeImage", 0.1f, frameRate);
}

private void ChangeImage()
{
    if (currentImage == frames.Length - 1)
    {
        CancelInvoke("ChangeImage");
        Destroy(explosion);
    }
    currentImage += 1;
    explosion.sprite = frames[currentImage];
}

when i used this invokerepeating in other thread, it gave error this method only invoke in main thread. After calling this scene, i didn't go back to any other scene because my all code is going to be stuck. Help me solve this problem!!

derHugo
  • 83,094
  • 9
  • 75
  • 115

1 Answers1

1

this is the correct way to do it without the use of threads

void Start()
{
    currentImage = 0;
    InvokeRepeating("ChangeImage", 0.1f, frameRate);
}

private void ChangeImage()
{

        if (currentImage == frames.Length - 1)
        {
            CancelInvoke("ChangeImage");
            Destroy(explosion);
        }
        currentImage += 1;
        explosion.sprite = frames[currentImage];

}
Ido Ben Shalom
  • 562
  • 4
  • 12