Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by ozarka30 · Jan 22, 2015 at 07:34 PM · errorgetcomponentunity 4awake

get_enabled can only be called from the main thread.

Hey there,

I'm getting this error, and I can't understand why.

get_enabled 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.

Here is my code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using Parse;
 
 
 public class HSManagerN : MonoBehaviour
 {
     public int skip = 0;
         
     void Awake ()
     {
         Text text;
         text = GetComponent <Text> ();  //This is what is causing the error
         
         var query = ParseObject.GetQuery("GameScore")
             .OrderBy("CreatedAt")
                 .ThenByDescending("Score");
         query = query.Skip(skip);
         query.FirstAsync().ContinueWith(t =>
                                         {
             ParseObject obj = t.Result;
             text.text = obj.Get<string>("Name");
         });        
     }    
     void Update ()
     {
         
     }
 }

I have the GetComponent in the awake function, but i'm still getting this error.

Does anyone know why this is happening?

Thanks for the help =)

Comment
Add comment · Show 2
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 DanSuperGP · Jan 22, 2015 at 11:00 PM 0
Share

Does this still happen if you change it to

 Text text - GetComponent<Text>();
avatar image KNDL · May 05, 2015 at 10:45 AM 0
Share

Same here...

2 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by NathanHold · May 05, 2015 at 12:19 PM

 text.text = obj.Get<string>("Name");


Is not thread safe. You must put it outside of the asynchronous call.

Comment
Add comment · Show 4 · 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 Bunny83 · May 05, 2015 at 12:36 PM 2
Share

Right. Generally nothing from the UnityEngine namespace is threadsafe in the first place. There are just a few exceptions (mostly structs like Vector2/3/4, $$anonymous$$atrix4x4, RaycastHit, Color, ...). All classes aren't threadsafe.

Unity actually uses properties for almost everything. $$anonymous$$ost of these properties actually call methods in native code. So you can't query the name, position, rotation, tag, layer, .... in another thread, only from the main thread.

Specifically Unity complains about you trying to read the "enabled" property from another thread.

avatar image NathanHold · May 05, 2015 at 12:48 PM 0
Share

You are correct, which is why Debug.Log is fine.

avatar image janoonk · Sep 30, 2016 at 01:28 PM 1
Share

Got the same problem/error. In my case I was sending and receiving $$anonymous$$QTT messages and I wanted to display the messages I received. I moved the Unity GUI code outside this $$anonymous$$essageReceived event. I stored the messages received in a List and in my GUI code in Update() I read from this List to display any message I had received.

avatar image Seraphic572 janoonk · Sep 12, 2017 at 06:23 AM 0
Share

Is there any possible better way of doing this ? Considering optimizations

avatar image
0

Answer by hav_ngs_ru · Jan 22, 2015 at 11:20 PM

maybe you have conflict with Text or smth like this? Try to avoid using name Text (use MyText or smth else).

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 ozarka30 · Jan 23, 2015 at 12:38 AM 0
Share

I tried putting the Text in front ins$$anonymous$$d of instantiating it beforehand. Nothing changed though :/

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

8 People are following this question.

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

Related Questions

Problem calling variable across scripts (Closed) 1 Answer

error CS0118: `New_Career.Fame' is a `field' but a `type' was expected 1 Answer

Cant add bullet trail material to script - > Bullet Trail Prefeb 0 Answers

C# GetComponent()... Not finding script problem. 1 Answer

"Object reference not set to an instance of an object" Error 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