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 puttly · Jan 20, 2013 at 06:40 PM · c#threadingdelegates

Delegates and "__ Can only be called from main thread"

Hey folks,

So I've run into a bit of a problem.

I'd like to use delegates and event handling in my Unity project to make my workflow much cleaner - however whenever an event is handle and I want it to do something I receive the error "___ Can only be called from the main thread. "

Does anyone have any experience with this? Work around?

 Class Network()
 
 public event ActionHandler Action;
 public delegate void ActionHandler(string[] action);
 
 
 private void ProcessAction(string[] message)
 {
     Action(message);
 }
 
 
 Class ActionManager()
 
 void Start()
 {
         Network.Instance.Action += new Network.ActionHandler(OnActionReceived);
     }
 
 public void Action(string[] action)
 {
      if action[0] == 1
      {
        OpenDoor()
       }
 
 }
 
 
 void OpenDoor()
 {
   this.animation.CrossFade("open_glassdoor");
 }
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

2 Replies

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

Answer by Bunny83 · Jan 20, 2013 at 07:53 PM

Delegates are no problem, it depends on from where you invokre them. Unity uses a single thread for the whole scripting environment, that simplifies everything a lot. That means the whole Unity API is not thread safe. So whenever you access an object from unity or use a method from Unity it has to be invoked from the main thread.

So all depends on where you execute your delegate (the ProcessAction function).

(Btw i guess your "OnActionReceived" should be "Action" or the other way round)

Beside that you shouldn't name your class Network since Unity already defines a class Network.

Comment
Add comment · 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
0

Answer by focuspark · Jan 21, 2013 at 10:02 PM

This the old STA vs MTA problem. STA does not allow memory allocated on a thread from being manipulated by another thread. It's ham handed attempt at thread safety, but it is what it is.

Comment
Add comment · Show 1 · 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 bleater · Dec 02, 2014 at 12:08 AM 0
Share

For those not familiar with the acronyms, STA -> "single threaded apartment", $$anonymous$$TA -> "multi-threaded apartment"... from CO$$anonymous$$ ter$$anonymous$$ology.

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Run return method on new thread? 2 Answers

Help filtering objects using delegates, passing methods to methods for delegate instancing 1 Answer

Unity 5.4.1 OnLevelWasLoaded Workaround for Singletons? 2 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