Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sovredcat · Feb 15, 2014 at 03:24 PM · c#2dspritestimerthread

C# timers - SetSprite_INTERNAL can only be called from the main thread.

So I'm having some issues with setting sprites using timers in C#/unity, here is my script http://pastie.org/8736350

Now the countdown itself works. The issue is that it doesn't want to change the sprite and gives me the following error:

SetSprite_INTERNAL can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

Does this means it wants me to move:

     countdown.Interval = 1000;
     countdown.Elapsed += new ElapsedEventHandler(timerproc);
     countdown.Enabled = true;

Under Start()? Cause I do not want that. I want the timer only to run once and then I want to Stop() it. Why is this ElapsedEventHandler running in a different thread?

I don't understand and if someone could explain that would be greatly appriciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Feb 15, 2014 at 03:31 PM

Timers is using another thread - you have two choices:

  1. Sync your calls to the main thread by sticking them in a delegate that gets called from an Update or Coroutine

  2. Use a coroutine for this, it's what they are good at!

Code:

 using UnityEngine;
 using System.Collections;
 
 public class CountDown : MonoBehaviour 
 {
     public Sprite countdownThree;
     public Sprite countdownTwo;
     public Sprite countdownOne;
     public Sprite countdownGo;
     SpriteRenderer countdownNumber;
     
     int countdownTime = 5;
 
     void Start () 
     {
         countdownNumber = gameObject.GetComponent<SpriteRenderer>();
         countdownNumber.sprite = null;
     }
 
     void Update () 
     {
         if(Input.GetKeyDown ("x"))
         {
             StartCoroutine(StartCountDown());
         }
     }
 
     public IEnumerator StartCountDown()
     {
         countdownNumber.enabled = true;
         countdownNumber.sprite = countdownThree;
         yield return new WaitForSeconds(1);
         countdownNumber.sprite = countdownTwo;
         yield return new WaitForSeconds(1);
         countdownNumber.sprite = countdownOne;
         yield return new WaitForSeconds(1);
         countdownNumber.sprite = countdownGo;
         yield return new WaitForSeconds(1);
         countdownNumber.enabled = false;

     }
 
   
 
 }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Feb 15, 2014 at 03:49 PM 0
Share

Or probably more flexible:

 using UnityEngine;
 using System.Collections;
  
 public class CountDown : $$anonymous$$onoBehaviour 
 {
     public Sprite[] countDownSprites;
     SpriteRenderer countdownNumber;
  
  
     void Start () 
     {
        countdownNumber = gameObject.GetComponent<SpriteRenderer>();
        countdownNumber.sprite = null;
     }
  
     void Update () 
     {
        if(Input.Get$$anonymous$$eyDown ("x"))
        {
          StartCoroutine(StartCountDown());
        }
     }
  
     public IEnumerator StartCountDown()
     {
         countdownNumber.enabled = true;
         for(var i = 0; i < countDownSprites.Length; i++) {
             countdownNumber.sprite = countDownSprites[i];
             yield return new WaitForSeconds(1);
         }
         countdownNumber.enabled = false;
  
     }
  
  
  
 }
     
avatar image sovredcat · Feb 15, 2014 at 04:33 PM 0
Share

Thank you both, I will look into this and try to understand the code.

avatar image sovredcat · Feb 15, 2014 at 04:39 PM 0
Share

@whydoidoit I am getting this error with your code: Assets/Scripts/Level 1/CountDown.cs(47,53): error CS1061: Type UnityEngine.Sprite[]' does not contain a definition for length' and no extension method length' of type UnityEngine.Sprite[]' could be found (are you missing a using directive or an assembly reference?) Edit: It's suppose to be Lenght not lenght :)

avatar image whydoidoit · Feb 15, 2014 at 04:40 PM 0
Share

Sorry it's Length

avatar image whydoidoit · Feb 15, 2014 at 04:40 PM 0
Share

Updated the comment.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip Sprite C# 0 Answers

Multiple Cars not working 1 Answer

WhY the exact same object behaves different ? 0 Answers

Using Texture Assets in Code 1 Answer

How do I make sprite renderer's sprite change in runtime c#? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges