- Home /
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 =)
Does this still happen if you change it to
Text text - GetComponent<Text>();
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.
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.
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.
Is there any possible better way of doing this ? Considering optimizations
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).
I tried putting the Text in front ins$$anonymous$$d of instantiating it beforehand. Nothing changed though :/
Your answer
Follow this Question
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