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!!

3
  • 1
    Unity's API is not thread safe, which means in order to use methods inside unity's api you must go through the main thread, for what purpose are you using that thread? Commented Oct 29, 2017 at 11:03
  • this code continuous change the picture to make the gif sense Commented Oct 29, 2017 at 12:47
  • then you don't need a thread for that, check out the answer below Commented Oct 29, 2017 at 12:57

1 Answer 1

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]; } 
Sign up to request clarification or add additional context in comments.

1 Comment

i said: this script stuck other script to run . script behind button didnt work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.