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
2
Question by Nhjufy · Feb 23, 2013 at 01:52 PM · errorthreadingmultithreadingthreadmain thread

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

Here's my code: http://pastebin.com/rXm7AtDw

I keep getting this error:

 get_transform  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.
Comment
Add comment · Show 4
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 fafase · Feb 23, 2013 at 03:14 PM 0
Share

I am not familiar with multi-threading in Unity but as the error says, you cannot use a transform outside the main thread. You are trying to modify it in a secondary thread. That is probably to avoid conflicts between thread. Also, the Input you use Get$$anonymous$$ey will probably call many threads since you are unlikely to be pressing for only one frame. Use Get$$anonymous$$eyDown ins$$anonymous$$d. But that won't get you to use transform in a secondary thread.

avatar image Nhjufy · Feb 23, 2013 at 04:03 PM 0
Share

But I really need to keep it running in background (multithreading), how can I do it?? :/

avatar image AVividLight · Feb 23, 2013 at 04:16 PM 0
Share

Why do you need to do a transform in a different thread?

avatar image Nhjufy · Feb 23, 2013 at 04:30 PM 0
Share

Because I need to keep moving whole time, one by one, in background...

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer Wiki

Answer by Henrik Poulsen · Feb 23, 2013 at 04:35 PM

The closest you can get to threading with the native unity3d functionality is [coroutines], since Unity isn't thread safe. If you know you need to do tasks that take a long time to compute you need to work around these limitations, if you feel you absolutely need to use multithreading. For example you might create a thread that calculates the data and then stores it and flags to the main thread that the data is ready to be processed and then you let the main thread set it.

However for the example you wrote I would probably do the following instead:

 void Update () {
     if(Input.GetKey (KeyCode.Space)) {
         Cube.transform.position += transform.right;
     }
 }

Since GetKey() returns true as long as the key is pressed then it will do the offset every frame anyway. So there is no reason for threading in your situation.

[1]

[1]: http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

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 Nhjufy · Feb 23, 2013 at 04:48 PM 0
Share

Thanks for idea, I'll add code to the update() and use informations from thread :)

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

13 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

Related Questions

Job System without using the main thread 1 Answer

Unity Backgroundworker threading 2 Answers

Thread::CurrentThreadIsMainThread() 0 Answers

Threaded procedural animation 0 Answers

Error joining threads 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