Hello Guest

Author Topic: tk2d TextMesh not working in a foreach loop  (Read 3847 times)

Nexcapto

  • Newbie
  • *
  • Posts: 2
    • View Profile
tk2d TextMesh not working in a foreach loop
« on: November 07, 2013, 05:56:21 am »
Hello, I'm really hoping I can get help here, but this question is a combination of tk2d and the Parse library (for databasing). Anyways, here is my code.
Code: [Select]
         ParseQuery<ParseObject> query = ParseObject.GetQuery("GameScore");
textMesh = GetComponent<tk2dTextMesh>();
string str = "          ";
query.FindAsync().ContinueWith(t =>
{
            IEnumerable<ParseObject> results = t.Result;
foreach (var result in results) {
//var textMesh = GetComponent<tk2dTextMesh>();
//var textMesh = Component<tk2dTextMesh>();
Debug.Log("Score: " + result["score"] + " and Name: " +                   result["playerName"]);

int temp = result.Get<int>("score");
str += "Score: " + temp.ToString();
str += "Name:  " + result.Get<string>("playerName");
Debug.Log("Got here");
1 textMesh.text += "Score: " + temp.ToString();
Debug.Log("Got here");
2 textMesh.Commit();

Now, my problem is when I try to write text to the textMesh and commit it.
I get some weird error "get_isPlaying 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."

BUT I know that it is the textMesh method, because when I move it or comment it out, the error disappears. What I am trying to do, is get my high scores from my parse database, and display them in the textMesh. The string I used to try and compound the text i want, and then add that string to the textMesh after the foreach loop, but the data is null afterwards.

The debug that displays the score and name from result does display properly, so i know that the data is coming in.
« Last Edit: November 07, 2013, 06:30:52 am by Nexcapto »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d TextMesh not working in a foreach loop
« Reply #1 on: November 07, 2013, 10:15:35 am »
Its looks like query.FindAsync() - I don't know how that works but if you're getting complaints about threading then it is probably running that delegate from another thread. Unity functions can only be called from the main thread, so you'll have to cache the results and run them on the main thread. Eg. fill up a list from the asnyc query, and use up the array in a Unity Update / LateUpdate function.

Nexcapto

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: tk2d TextMesh not working in a foreach loop
« Reply #2 on: November 08, 2013, 01:40:49 am »
Alright, so I figured out what I actually needed to do. (I realize this was hard to answer because the commands about querys are not part of tk2d and therefore you really shouldn't have to answer my questions about it)

I need to write a co-routine for getting one result from my IENumerator at a time, then add the string and integer values from that gameObject to my textMesh.text, and then finally commit this.

Thank you for the attempted help, much appreciated. You guys are very fast at replying to questions on here, and I will be sure to post problems specifically related to tk2d next time. This time i was unsure what was wrong, and spent a decent amount of time researching with null results. More research yielded my solution.

Thanks, Nexcapto