Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by pragmascript · May 31, 2011 at 10:23 AM · threadinginvokemultithreading

MonoBehaviour.Invoke and Threading

I'm trying to invoke a method from a different thread using MonoBehaviour.Invoke.

But i get weird errors:

 UnityEngine.MonoBehaviour:Invoke(String, Single)
 NetworkClient:<createJoinRoom>m__1(Connection) (at Assets\Scripts\Multiplayer\NetworkClient.cs:90)
 PlayerIOClient.c__DisplayClass6:<.ctor>b__1(Object, Message)
 PlayerIOClient.MsgConnection:GotMessage(piov)
 pioq:c(piov)
 piov:a(Byte[], Int32, Int32, b, pioq)
 pioad:a(Byte[], Int32, Int32)
 pios:c(IAsyncResult)
 System.Net.Sockets.SocketAsyncResult:Complete()
 System.Net.Sockets.Worker:Receive()

Isn't the "invoke" method supposed to be called from other threads? If not what can I use to execute a method on the main Unity thread?

edit: ok here is my workaround to this issue: http://pastebin.com/wJ52J3mE

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
3
Best Answer

Answer by Mike 3 · May 31, 2011 at 11:07 AM

There isn't any built in synchronization between user threads and the unity thread. The main issue is a more to do with timing of events rather than the usual synchronization of data issue.

The way I've solved it in the past is to make a synchronization class which takes a delegate and some user data, and places both into a list. That list is then iterated over in Update, with all the delegates being called, with the user data being passed as parameter.

The calling from update part is what makes it work at any rate, since unity expects you to only call into the unity api from its own events

Comment
Add comment · Show 3 · 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 pragmascript · May 31, 2011 at 11:27 AM 0
Share

ok thank you. I will do that. seems to introduce some overhead due to locking and polling in the update method so.

avatar image Mike 3 · May 31, 2011 at 11:43 AM 0
Share

Indeed, but it's much better than throwing threading errors :)

avatar image pragmascript · May 31, 2011 at 12:35 PM 0
Share

much better :)

avatar image
0

Answer by Tom 17 · May 31, 2011 at 11:10 AM

Simply don't use Unity API from a seperate thread. You need to use the functionality that Unity has as coroutines and such. All calls to the engine stuff needs to be synchronized on the main thread, no way around it, no seperate system threads to directly call the API. I did too stumble over it just recently.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multi threading problem ! 2 Answers

"...can only be called from the main thread" - MultiThreading 1 Answer

How to check if code is running on the unity thread? 1 Answer

Multithreading freezes editor 1 Answer

How can I get a component data from an entity inside a JobComponentSystem? 1 Answer


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