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
5
Question by kosmos · Nov 24, 2011 at 09:31 PM · gameobjectaccessthread

How to execute method in main thread from another threads?

i see it hard to find any adequately answer on that.

i have a TcpClient that accept data in separate thread and should update gameobject state basing on received data.

first of all i can't call GameObject.Find("bla") from another thread. Well i though it's not a big problems and tried to use MethodInfo variable pointed on needed function, to execute it like this when received:

method_info.Invoke(this, new object[] { data }); //for weird reason it executes in current thread (not main)

ok... let's see then at this.Invoke("NeededFunction", 0); //what O_o? i can call it only in main thread? nice invoke

ok... let's see on delegates... err this does not contain InvokeRequred field and this is already a big problem

omg just how to access gameobjects from another threads?

Comment
Add comment · Show 1
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 jmunozar · Feb 24, 2017 at 05:35 PM 0
Share

Hi @kosmos, sorry for reviving this necro question, did you managed to solve this issue?, I'm right now facing the same problem and havent been able to find a solution :/

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by hrgchris · Feb 24, 2017 at 07:58 PM

Hi there

The correct approaches for this sort of thing vary depending on the task. You can not simply 'call a method on another thread' - you have to ask the other thread to call it.

I have found the most elegant approach in unity is to use the c# lock keyword to control access to a .net queue. I can add 'events' , which may be structures or delegates into my queue from a thread. Then on the main thread in an update function I can process the queue.

You can't access game objects and stuff from other threads - it's single threaded by design.

  • Chris

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 jmunozar · Feb 24, 2017 at 08:23 PM 0
Share

Hey @hrgchris, Thanks for the reply!

yeah, that's what I have seen (queue processing), but what happens when you need to execute it in the mainthread on an AssetPostProcessor function (I'm writing an importer) where you dont have a loop to process the queue :(?, how would you get around on that?

avatar image Bunny83 jmunozar · Feb 24, 2017 at 08:39 PM 0
Share

You can not simply "run code on another thread". A thread is a single sequencial execution unit. The code inside one thread is run in sequence. If you don't have something somewhere inside that sequence that runs code (like reading some method from a queue) you can't execute it on that thread.

The main loop of the main thread is controlled by Unity itself. Unity provides certain callbacks (like Update, ...) which allows you to put your code inside the main loop.

I think most AssetPostProcessor callbacks should actually be called from the main thread. The only exception might be "OnPostprocessAllAssets". If you need to queue things on the main thread from an editor script, you can use the generic update delegate of the EditorApplication class. That update is called about 100 times per second while the editor is active.

How you would implement that highly depends on your actual code and usage and does not belong here on this question. If you have a question, ask a seperate question and add more details.

avatar image jmunozar Bunny83 · Feb 24, 2017 at 08:42 PM 0
Share

That was basically what I was looking for, thanks! :) <3

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it OK to use public accessers for unique gameobjects (C#) ? 3 Answers

How can I access other scripts and their functions? 3 Answers

How do you access a script if you don't know it's name? 5 Answers

Whats the best way to access other Game Objects? 2 Answers

Access objects enum type 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